省钱达人

PhotoContainerView.m 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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.backgroundColor = [UIColor yhGrayColor];
  68. imageView.frame = CGRectMake(columnIndex * (itemW + margin), rowIndex * (itemH + margin), itemW, itemH);
  69. }];
  70. CGFloat w = perRowItemCount * itemW + (perRowItemCount - 1) * margin;
  71. int columnCount = ceilf(_picPathStringsArray.count * 1.0 / perRowItemCount);
  72. CGFloat h = columnCount * itemH + (columnCount - 1) * margin;
  73. self.width = w;
  74. self.height = h;
  75. self.fixedHeight = @(h);
  76. self.fixedWith = @(w);
  77. }
  78. #pragma mark - private actions
  79. - (void)tapImageView:(UITapGestureRecognizer *)tap
  80. {
  81. UIView *imageView = tap.view;
  82. if (self.delegate &&[self.delegate respondsToSelector:@selector(otherOPByModel:)]) {
  83. [self.delegate otherOPByModel:self.modelArray[imageView.tag]];
  84. }else{
  85. SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
  86. browser.currentImageIndex = imageView.tag;
  87. browser.sourceImagesContainerView = self;
  88. browser.imageCount = self.picPathStringsArray.count;
  89. browser.delegate = self;
  90. [browser show];
  91. }
  92. }
  93. - (CGFloat)itemWidthForPicPathArray:(NSArray *)array
  94. {
  95. if (array.count == 1) {
  96. return 120;
  97. } else {
  98. CGFloat margin = 5;
  99. CGFloat w = ([UIScreen mainScreen].bounds.size.width-20-margin*2)/3;
  100. return w;
  101. }
  102. }
  103. - (NSInteger)perRowItemCountForPicPathArray:(NSArray *)array
  104. {
  105. // if (array.count < 3) {
  106. // return array.count;
  107. // } else if (array.count <= 4) {
  108. // return 2;
  109. // } else {
  110. // return 3;
  111. // }
  112. if (array.count<=3) {
  113. return array.count;
  114. }
  115. return 3;
  116. }
  117. #pragma mark - SDPhotoBrowserDelegate
  118. - (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index
  119. {
  120. NSString *imageName = self.picPathStringsArray[index];
  121. NSURL *url = [[NSBundle mainBundle] URLForResource:imageName withExtension:nil];
  122. return url;
  123. }
  124. - (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index
  125. {
  126. UIImageView *imageView = self.subviews[index];
  127. return imageView.image;
  128. }
  129. @end