YHWorkGroupPhotoContainer.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //
  2. // YHWorkGroupPhotoContainer.m
  3. // HKPTimeLine 仿赤兔、微博动态
  4. // CSDN: http://blog.csdn.net/samuelandkevin
  5. // Created by samuelandkevin on 16/9/20.
  6. // Copyright © 2016年 HKP. All rights reserved.
  7. //
  8. #import "YHWorkGroupPhotoContainer.h"
  9. #import "SDPhotoBrowser.h"
  10. @interface YHWorkGroupPhotoContainer ()<SDPhotoBrowserDelegate>
  11. @property (nonatomic, strong) NSArray *imageViewsArray;
  12. @property (nonatomic, assign) CGFloat width;
  13. @end
  14. @implementation YHWorkGroupPhotoContainer
  15. - (instancetype)initWithWidth:(CGFloat)width{
  16. if (self = [super init]) {
  17. NSAssert(width>0, ASLocalizedString(@"请设置图片容器的宽度"));
  18. self.width = width;
  19. }
  20. return self;
  21. }
  22. - (instancetype)initWithFrame:(CGRect)frame
  23. {
  24. if (self = [super initWithFrame:frame]) {
  25. [self setup];
  26. }
  27. return self;
  28. }
  29. - (void)awakeFromNib{
  30. [self setup];
  31. [super awakeFromNib];
  32. }
  33. - (void)setup
  34. {
  35. NSMutableArray *temp = [NSMutableArray new];
  36. for (int i = 0; i < 9; i++) {
  37. UIImageView *imageView = [UIImageView new];
  38. imageView.layer.cornerRadius = 8;
  39. imageView.layer.masksToBounds = YES;
  40. [self addSubview:imageView];
  41. imageView.userInteractionEnabled = YES;
  42. imageView.tag = i;
  43. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];
  44. [imageView addGestureRecognizer:tap];
  45. [temp addObject:imageView];
  46. }
  47. self.imageViewsArray = [temp copy];
  48. }
  49. - (CGFloat)setupPicUrlArray:(NSArray *)picUrlArray{
  50. _picUrlArray = picUrlArray;
  51. for (long i = _picUrlArray.count; i < self.imageViewsArray.count; i++) {
  52. UIImageView *imageView = [self.imageViewsArray objectAtIndex:i];
  53. imageView.hidden = YES;
  54. }
  55. if (_picUrlArray.count == 0) {
  56. return 0;
  57. }
  58. CGFloat itemW = [self itemWidthForPicPathArray:_picUrlArray];
  59. CGFloat itemH = itemW;
  60. long perRowItemCount = [self perRowItemCountForPicPathArray:_picUrlArray];
  61. CGFloat margin = 5;
  62. if ([_picUrlArray isKindOfClass:[NSArray class]]) {
  63. for (int i = 0; i< _picUrlArray.count; i++) {
  64. NSURL *obj = _picUrlArray[i];
  65. // [NSURL URLWithString:_picOriArray[i]];
  66. // if ([_picOriArray[i] isKindOfClass:[NSString class]]) {
  67. // obj = [NSURL URLWithString:_picOriArray[i]];
  68. // }else if([_picOriArray[i] isKindOfClass:[NSURL class]]){
  69. // obj = _picOriArray[i];
  70. //// [NSURL URLWithString:_picOriArray[i]];
  71. // }
  72. // _picUrlArray[i];
  73. long columnIndex = i % perRowItemCount;
  74. long rowIndex = i / perRowItemCount;
  75. UIImageView *imageView = self.imageViewsArray[i];
  76. imageView.contentMode = UIViewContentModeScaleAspectFill;
  77. imageView.clipsToBounds = YES;
  78. imageView.hidden = NO;
  79. [imageView sd_setImageWithURL:obj placeholderImage:[UIImage imageNamed:@"workgroup_img_defaultPhoto"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
  80. if (image.size.width < itemW || image.size.height < itemW) {
  81. imageView.contentMode = UIViewContentModeScaleAspectFit;
  82. }
  83. }];
  84. imageView.frame = CGRectMake(columnIndex * (itemW + margin), rowIndex * (itemH + margin), itemW, itemH);
  85. }
  86. }
  87. int columnCount = ceilf(_picUrlArray.count * 1.0 / perRowItemCount);
  88. CGFloat h = columnCount * itemH + (columnCount - 1) * margin;
  89. return h;
  90. }
  91. #pragma mark - private actions
  92. - (void)tapImageView:(UITapGestureRecognizer *)tap
  93. {
  94. UIView *imageView = tap.view;
  95. SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
  96. browser.currentImageIndex = imageView.tag;
  97. browser.sourceImagesContainerView = self;
  98. browser.imageCount = self.picUrlArray.count;
  99. browser.delegate = self;
  100. [browser show];
  101. }
  102. - (CGFloat)itemWidthForPicPathArray:(NSArray *)array
  103. {
  104. if (array.count == 1) {
  105. return 120;
  106. } else {
  107. CGFloat itemW = (self.width -10) /3 ;
  108. return itemW;
  109. }
  110. }
  111. - (NSInteger)perRowItemCountForPicPathArray:(NSArray *)array
  112. {
  113. if (array.count < 3) {
  114. return array.count;
  115. } else if (array.count == 4) {
  116. return 2;
  117. } else {
  118. return 3;
  119. }
  120. }
  121. #pragma mark - SDPhotoBrowserDelegate
  122. - (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index
  123. {
  124. NSURL *url = [NSURL new];
  125. if (index < self.picOriArray.count) {
  126. url = [NSURL URLWithString:self.picOriArray[index]];
  127. }
  128. return url;
  129. }
  130. - (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index
  131. {
  132. UIImageView *imageView = self.subviews[index];
  133. return imageView.image;
  134. }
  135. /*
  136. // Only override drawRect: if you perform custom drawing.
  137. // An empty implementation adversely affects performance during animation.
  138. - (void)drawRect:(CGRect)rect {
  139. // Drawing code
  140. }
  141. */
  142. @end