口袋优选

PhotoContainerView.m 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //
  2. // PhotoContainerView.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/5/16.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "PhotoContainerView.h"
  9. #import "SDPhotoBrowser.h"
  10. #import "UIView+SDAutoLayout.h"
  11. @interface PhotoContainerView ()<SDPhotoBrowserDelegate>
  12. @property (nonatomic, strong) NSArray *imageViewsArray;
  13. @end
  14. @implementation PhotoContainerView
  15. - (instancetype)initWithFrame:(CGRect)frame
  16. {
  17. if (self = [super initWithFrame:frame]) {
  18. [self setup];
  19. }
  20. return self;
  21. }
  22. - (void)setup
  23. {
  24. NSMutableArray *temp = [NSMutableArray new];
  25. for (int i = 0; i < 9; i++) {
  26. UIImageView *imageView = [UIImageView new];
  27. [self addSubview:imageView];
  28. imageView.userInteractionEnabled = YES;
  29. imageView.tag = i;
  30. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];
  31. [imageView addGestureRecognizer:tap];
  32. [temp addObject:imageView];
  33. }
  34. self.imageViewsArray = [temp copy];
  35. }
  36. - (void)setPicPathStringsArray:(NSArray *)picPathStringsArray
  37. {
  38. _picPathStringsArray = picPathStringsArray;
  39. for (long i = _picPathStringsArray.count; i < self.imageViewsArray.count; i++) {
  40. UIImageView *imageView = [self.imageViewsArray objectAtIndex:i];
  41. imageView.hidden = YES;
  42. }
  43. if (_picPathStringsArray.count == 0) {
  44. self.height = 0;
  45. self.fixedHeight = @(0);
  46. return;
  47. }
  48. CGFloat itemW = [self itemWidthForPicPathArray:_picPathStringsArray];
  49. CGFloat itemH = 0;
  50. // if (_picPathStringsArray.count == 1) {
  51. // UIImage *image = [UIImage imageNamed:_picPathStringsArray.firstObject];
  52. // if (image.size.width) {
  53. // itemH = image.size.height / image.size.width * itemW;
  54. // }
  55. // } else {
  56. // itemH = itemW;
  57. // }
  58. itemH = itemW;
  59. long perRowItemCount = [self perRowItemCountForPicPathArray:_picPathStringsArray];
  60. CGFloat margin = 5;
  61. [_picPathStringsArray enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  62. long columnIndex = idx % perRowItemCount;
  63. long rowIndex = idx / perRowItemCount;
  64. UIImageView *imageView = [_imageViewsArray objectAtIndex:idx];
  65. imageView.hidden = NO;
  66. // [imageView yy_setImageWithURL:[NSURL URLWithString:obj] options:YYWebImageOptionProgressiveBlur | YYWebImageOptionSetImageWithFadeAnimation];
  67. [imageView sd_setFadeImageWithURL:[NSURL URLWithString:obj] placeholderImage:nil options:0 progress:nil completed:nil];
  68. imageView.backgroundColor = [UIColor yhGrayColor];
  69. imageView.frame = CGRectMake(columnIndex * (itemW + margin), rowIndex * (itemH + margin), itemW, itemH);
  70. }];
  71. CGFloat w = perRowItemCount * itemW + (perRowItemCount - 1) * margin;
  72. int columnCount = ceilf(_picPathStringsArray.count * 1.0 / perRowItemCount);
  73. CGFloat h = columnCount * itemH + (columnCount - 1) * margin;
  74. self.width = w;
  75. self.height = h;
  76. self.fixedHeight = @(h);
  77. self.fixedWith = @(w);
  78. }
  79. #pragma mark - private actions
  80. - (void)tapImageView:(UITapGestureRecognizer *)tap
  81. {
  82. UIView *imageView = tap.view;
  83. if (self.delegate &&[self.delegate respondsToSelector:@selector(otherOPByModel:)]) {
  84. [self.delegate otherOPByModel:self.modelArray[imageView.tag]];
  85. }else{
  86. SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
  87. browser.currentImageIndex = imageView.tag;
  88. browser.sourceImagesContainerView = self;
  89. browser.imageCount = self.picPathStringsArray.count;
  90. browser.delegate = self;
  91. [browser show];
  92. }
  93. }
  94. - (CGFloat)itemWidthForPicPathArray:(NSArray *)array
  95. {
  96. if (array.count == 1) {
  97. return 120;
  98. } else {
  99. CGFloat margin = 5;
  100. if(self.contentWidth==0){
  101. self.contentWidth=[UIScreen mainScreen].bounds.size.width;
  102. }
  103. CGFloat w = (self.contentWidth-20-margin*2)/3;
  104. return w;
  105. }
  106. }
  107. - (NSInteger)perRowItemCountForPicPathArray:(NSArray *)array
  108. {
  109. // if (array.count < 3) {
  110. // return array.count;
  111. // } else if (array.count <= 4) {
  112. // return 2;
  113. // } else {
  114. // return 3;
  115. // }
  116. if (array.count<=3) {
  117. return array.count;
  118. }
  119. return 3;
  120. }
  121. #pragma mark - SDPhotoBrowserDelegate
  122. - (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index
  123. {
  124. NSString *imageName = self.picPathStringsArray[index];
  125. NSURL *url = [[NSBundle mainBundle] URLForResource:imageName withExtension:nil];
  126. return url;
  127. }
  128. - (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index
  129. {
  130. UIImageView *imageView = self.subviews[index];
  131. return imageView.image;
  132. }
  133. @end