123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- #import "SDPhotoGroup.h"
- #import "SDPhotoItem.h"
- #import "UIButton+WebCache.h"
- #import "SDPhotoBrowser.h"
- #define SDPhotoGroupImageMargin 15
- @interface SDPhotoGroup () <SDPhotoBrowserDelegate>
- @end
- @implementation SDPhotoGroup
- - (id)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
-
- [[SDWebImageManager sharedManager].imageCache clearMemory];
- }
- return self;
- }
- - (void)setPhotoItemArray:(NSArray *)photoItemArray
- {
- _photoItemArray = photoItemArray;
- [photoItemArray enumerateObjectsUsingBlock:^(SDPhotoItem *obj, NSUInteger idx, BOOL *stop) {
- UIButton *btn = [[UIButton alloc] init];
- [btn sd_setImageWithURL:[NSURL URLWithString:obj.thumbnail_pic] forState:UIControlStateNormal];
-
-
- btn.tag = idx;
- NSLog(@"====%lu",(unsigned long)idx);
-
- [btn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
- [self addSubview:btn];
- }];
- }
- - (void)layoutSubviews
- {
- [super layoutSubviews];
- int imageCount = (int) self.photoItemArray.count;
- int perRowImageCount = ((imageCount == 4) ? 2 : 3);
-
- int totalRowCount = imageCount / perRowImageCount + 0.99999;
- CGFloat w = 80;
- CGFloat h = 80;
-
- [self.subviews enumerateObjectsUsingBlock:^(UIButton *btn, NSUInteger idx, BOOL *stop) {
-
- int rowIndex = (int)( idx / perRowImageCount);
- int columnIndex = idx % perRowImageCount;
- CGFloat x = columnIndex * (w + SDPhotoGroupImageMargin);
- CGFloat y = rowIndex * (h + SDPhotoGroupImageMargin);
- btn.frame = CGRectMake(x, y, w, h);
- }];
- self.frame = CGRectMake(10, 10, 280, totalRowCount * (SDPhotoGroupImageMargin + h));
- }
- - (void)buttonClick:(UIButton *)button
- {
- SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
- browser.sourceImagesContainerView = self;
- browser.imageCount = self.photoItemArray.count;
- browser.currentImageIndex = (int) button.tag;
- browser.delegate = self;
- [browser show];
-
- }
- #pragma mark - photobrowser代理方法
- - (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index
- {
- return [self.subviews[index] currentImage];
- }
- - (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index
- {
- NSString *urlStr = [[self.photoItemArray[index] thumbnail_pic] stringByReplacingOccurrencesOfString:@"thumbnail" withString:@"bmiddle"];
- return [NSURL URLWithString:urlStr];
- }
- @end
|