// // PhotoContainerView.m // YouHuiProject // // Created by 小花 on 2018/5/16. // Copyright © 2018年 kuxuan. All rights reserved. // #import "PhotoContainerView.h" #import "SDPhotoBrowser.h" #import "UIView+SDAutoLayout.h" @interface PhotoContainerView () @property (nonatomic, strong) NSArray *imageViewsArray; @end @implementation PhotoContainerView - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [self setup]; } return self; } - (void)setup { NSMutableArray *temp = [NSMutableArray new]; for (int i = 0; i < 9; i++) { UIImageView *imageView = [UIImageView new]; [self addSubview:imageView]; imageView.userInteractionEnabled = YES; imageView.tag = i; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)]; [imageView addGestureRecognizer:tap]; [temp addObject:imageView]; } self.imageViewsArray = [temp copy]; } - (void)setPicPathStringsArray:(NSArray *)picPathStringsArray { _picPathStringsArray = picPathStringsArray; for (long i = _picPathStringsArray.count; i < self.imageViewsArray.count; i++) { UIImageView *imageView = [self.imageViewsArray objectAtIndex:i]; imageView.hidden = YES; } if (_picPathStringsArray.count == 0) { self.height = 0; self.fixedHeight = @(0); return; } CGFloat itemW = [self itemWidthForPicPathArray:_picPathStringsArray]; CGFloat itemH = 0; // if (_picPathStringsArray.count == 1) { // UIImage *image = [UIImage imageNamed:_picPathStringsArray.firstObject]; // if (image.size.width) { // itemH = image.size.height / image.size.width * itemW; // } // } else { // itemH = itemW; // } itemH = itemW; long perRowItemCount = [self perRowItemCountForPicPathArray:_picPathStringsArray]; CGFloat margin = 5; [_picPathStringsArray enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { long columnIndex = idx % perRowItemCount; long rowIndex = idx / perRowItemCount; UIImageView *imageView = [_imageViewsArray objectAtIndex:idx]; imageView.hidden = NO; // [imageView yy_setImageWithURL:[NSURL URLWithString:obj] options:YYWebImageOptionProgressiveBlur | YYWebImageOptionSetImageWithFadeAnimation]; [imageView sd_setFadeImageWithURL:[NSURL URLWithString:obj] placeholderImage:nil options:0 progress:nil completed:nil]; imageView.backgroundColor = [UIColor yhGrayColor]; imageView.frame = CGRectMake(columnIndex * (itemW + margin), rowIndex * (itemH + margin), itemW, itemH); }]; CGFloat w = perRowItemCount * itemW + (perRowItemCount - 1) * margin; int columnCount = ceilf(_picPathStringsArray.count * 1.0 / perRowItemCount); CGFloat h = columnCount * itemH + (columnCount - 1) * margin; self.width = w; self.height = h; self.fixedHeight = @(h); self.fixedWith = @(w); } #pragma mark - private actions - (void)tapImageView:(UITapGestureRecognizer *)tap { UIView *imageView = tap.view; if (self.delegate &&[self.delegate respondsToSelector:@selector(otherOPByModel:)]) { [self.delegate otherOPByModel:self.modelArray[imageView.tag]]; }else{ SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init]; browser.currentImageIndex = imageView.tag; browser.sourceImagesContainerView = self; browser.imageCount = self.picPathStringsArray.count; browser.delegate = self; [browser show]; } } - (CGFloat)itemWidthForPicPathArray:(NSArray *)array { if (array.count == 1) { return 120; } else { CGFloat margin = 5; if(self.contentWidth==0){ self.contentWidth=[UIScreen mainScreen].bounds.size.width; } CGFloat w = (self.contentWidth-20-margin*2)/3; return w; } } - (NSInteger)perRowItemCountForPicPathArray:(NSArray *)array { // if (array.count < 3) { // return array.count; // } else if (array.count <= 4) { // return 2; // } else { // return 3; // } if (array.count<=3) { return array.count; } return 3; } #pragma mark - SDPhotoBrowserDelegate - (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index { NSString *imageName = self.picPathStringsArray[index]; NSURL *url = [[NSBundle mainBundle] URLForResource:imageName withExtension:nil]; return url; } - (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index { UIImageView *imageView = self.subviews[index]; return imageView.image; } @end