| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- //
- // YHWorkGroupPhotoContainer.m
- // HKPTimeLine 仿赤兔、微博动态
- // CSDN: http://blog.csdn.net/samuelandkevin
- // Created by samuelandkevin on 16/9/20.
- // Copyright © 2016年 HKP. All rights reserved.
- //
- #import "YHWorkGroupPhotoContainer.h"
- #import "SDPhotoBrowser.h"
- @interface YHWorkGroupPhotoContainer ()<SDPhotoBrowserDelegate>
- @property (nonatomic, strong) NSArray *imageViewsArray;
- @property (nonatomic, assign) CGFloat width;
- @end
- @implementation YHWorkGroupPhotoContainer
- - (instancetype)initWithWidth:(CGFloat)width{
- if (self = [super init]) {
- NSAssert(width>0, ASLocalizedString(@"请设置图片容器的宽度"));
- self.width = width;
- }
- return self;
- }
- - (instancetype)initWithFrame:(CGRect)frame
- {
- if (self = [super initWithFrame:frame]) {
- [self setup];
- }
- return self;
- }
- - (void)awakeFromNib{
- [self setup];
- [super awakeFromNib];
- }
- - (void)setup
- {
- NSMutableArray *temp = [NSMutableArray new];
-
- for (int i = 0; i < 9; i++) {
- UIImageView *imageView = [UIImageView new];
- imageView.layer.cornerRadius = 8;
- imageView.layer.masksToBounds = YES;
- [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];
- }
- - (CGFloat)setupPicUrlArray:(NSArray *)picUrlArray{
- _picUrlArray = picUrlArray;
-
- for (long i = _picUrlArray.count; i < self.imageViewsArray.count; i++) {
- UIImageView *imageView = [self.imageViewsArray objectAtIndex:i];
- imageView.hidden = YES;
- }
-
- if (_picUrlArray.count == 0) {
- return 0;
- }
-
- CGFloat itemW = [self itemWidthForPicPathArray:_picUrlArray];
- CGFloat itemH = itemW;
-
- long perRowItemCount = [self perRowItemCountForPicPathArray:_picUrlArray];
- CGFloat margin = 5;
-
- if ([_picUrlArray isKindOfClass:[NSArray class]]) {
- for (int i = 0; i< _picUrlArray.count; i++) {
- NSURL *obj = _picUrlArray[i];
- // [NSURL URLWithString:_picOriArray[i]];
-
- // if ([_picOriArray[i] isKindOfClass:[NSString class]]) {
- // obj = [NSURL URLWithString:_picOriArray[i]];
- // }else if([_picOriArray[i] isKindOfClass:[NSURL class]]){
- // obj = _picOriArray[i];
- //// [NSURL URLWithString:_picOriArray[i]];
- // }
-
- // _picUrlArray[i];
- long columnIndex = i % perRowItemCount;
- long rowIndex = i / perRowItemCount;
-
- UIImageView *imageView = self.imageViewsArray[i];
- imageView.contentMode = UIViewContentModeScaleAspectFill;
- imageView.clipsToBounds = YES;
- imageView.hidden = NO;
- [imageView sd_setImageWithURL:obj placeholderImage:[UIImage imageNamed:@"workgroup_img_defaultPhoto"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
-
- if (image.size.width < itemW || image.size.height < itemW) {
- imageView.contentMode = UIViewContentModeScaleAspectFit;
- }
-
- }];
-
- imageView.frame = CGRectMake(columnIndex * (itemW + margin), rowIndex * (itemH + margin), itemW, itemH);
-
- }
- }
-
-
-
- int columnCount = ceilf(_picUrlArray.count * 1.0 / perRowItemCount);
- CGFloat h = columnCount * itemH + (columnCount - 1) * margin;
-
- return h;
- }
- #pragma mark - private actions
- - (void)tapImageView:(UITapGestureRecognizer *)tap
- {
- UIView *imageView = tap.view;
- SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
- browser.currentImageIndex = imageView.tag;
- browser.sourceImagesContainerView = self;
- browser.imageCount = self.picUrlArray.count;
- browser.delegate = self;
- [browser show];
- }
- - (CGFloat)itemWidthForPicPathArray:(NSArray *)array
- {
- if (array.count == 1) {
- return 120;
- } else {
- CGFloat itemW = (self.width -10) /3 ;
- return itemW;
- }
- }
- - (NSInteger)perRowItemCountForPicPathArray:(NSArray *)array
- {
- if (array.count < 3) {
- return array.count;
- } else if (array.count == 4) {
- return 2;
- } else {
- return 3;
- }
- }
- #pragma mark - SDPhotoBrowserDelegate
- - (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index
- {
- NSURL *url = [NSURL new];
- if (index < self.picOriArray.count) {
-
- url = [NSURL URLWithString:self.picOriArray[index]];
- }
- return url;
- }
- - (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index
- {
- UIImageView *imageView = self.subviews[index];
- return imageView.image;
- }
- /*
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect {
- // Drawing code
- }
- */
- @end
|