// // HUPhotoBrowser.m // HUPhotoBrowser // // Created by mac on 16/2/24. // Copyright (c) 2016年 jinhuadiqigan. All rights reserved. // #import "HUPhotoBrowser.h" #import "HUPhotoBrowserCell.h" #import "hu_const.h" //#import "HUWebImage.h" #import "ZCAlertSheetView.h" #import "HUToast.h" @interface HUPhotoBrowser () { CGRect _endTempFrame; NSInteger _currentPage; NSIndexPath *_zoomingIndexPath; BOOL _imageDidLoaded; BOOL _animationCompleted; } @property (nonatomic, strong) UIImageView *imageView; @property (nonatomic, strong) UIImageView *tmpImageView; @property (nonatomic, strong) UICollectionView *collectionView; @property (nonatomic, strong) UIView *toolBar; @property (nonatomic, weak) UILabel *countLab; @property (nonatomic, strong) NSArray *URLStrings; @property (nonatomic) NSInteger index; @property (nonatomic) NSInteger imagesCount; @property (nonatomic, copy) DismissBlock dismissDlock; @property (nonatomic, strong) NSArray *images; @property (nonatomic, strong) ZCAlertSheetView *alertView; @property (nonatomic,strong)NSMutableArray *newImages; @end @implementation HUPhotoBrowser - (NSMutableArray *)newImages{ if (!_newImages) { _newImages = [NSMutableArray array]; } return _newImages; } - (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo { if (!error) { [ZCHUDHelper showTitle:@"保存成功"]; }else{ [ZCHUDHelper showTitle:@"保存失败"]; } } - (void)dealloc { self.collectionView.delegate = nil; [[NSNotificationCenter defaultCenter] removeObserver:self]; } + (instancetype)showFromImageView:(UIImageView *)imageView withURLStrings:(NSArray *)URLStrings placeholderURLStrings:(NSArray *)holderURLStrings atIndex:(NSInteger)index dismiss:(DismissBlock)block { HUPhotoBrowser *browser = [[HUPhotoBrowser alloc] initWithFrame:kScreenRect Type:NormalType]; browser.imageView = imageView; browser.URLStrings = URLStrings; browser.imagesCount = URLStrings.count; [browser resetCountLabWithIndex:index+1]; [browser configureBrowser]; [browser animateImageViewAtIndex:index]; browser.placeHolderUrlStrings = holderURLStrings; browser.dismissDlock = block; return browser; } + (instancetype)showFromImageView:(UIImageView *)imageView withURLStrings:(NSArray *)URLStrings placeholderImage:(UIImage *)image atIndex:(NSInteger)index dismiss:(DismissBlock)block { HUPhotoBrowser *browser = [[HUPhotoBrowser alloc] initWithFrame:kScreenRect Type:NormalType]; browser.imageView = imageView; browser.URLStrings = URLStrings; browser.imagesCount = URLStrings.count; [browser resetCountLabWithIndex:index+1]; [browser configureBrowser]; [browser animateImageViewAtIndex:index]; browser.placeholderImage = image; browser.dismissDlock = block; return browser; } + (instancetype)showFromImageView:(UIImageView *)imageView withImages:(NSArray *)images atIndex:(NSInteger)index dismiss:(DismissBlock)block{ HUPhotoBrowser *browser = [[HUPhotoBrowser alloc] initWithFrame:kScreenRect Type:NormalType]; browser.imageView = imageView; browser.images = images; browser.imagesCount = images.count; [browser resetCountLabWithIndex:index+1]; [browser configureBrowser]; [browser animateImageViewAtIndex:index]; browser.dismissDlock = block; return browser; } + (nonnull instancetype)Type444showFromImageView:(nullable UIImageView *)imageView withImages:(nullable NSArray *)images atIndex:(NSInteger)index dismiss:(DismissBlock)block{ HUPhotoBrowser *browser = [[HUPhotoBrowser alloc] initWithFrame:kScreenRect Type:Type44444]; browser.imageView = imageView; browser.images = images; browser.imagesCount = images.count; [browser resetCountLabWithIndex:index+1]; [browser configureBrowser]; [browser animateImageViewAtIndex:index]; browser.dismissDlock = block; return browser; } + (instancetype)showFromImageView:(UIImageView *)imageView withURLStrings:(NSArray *)URLStrings atIndex:(NSInteger)index { return [self showFromImageView:imageView withURLStrings:URLStrings placeholderImage:nil atIndex:index dismiss:nil]; } + (nonnull instancetype)showFromImageView:(nullable UIImageView *)imageView withURLStrings:(nullable NSArray *)URLStrings placeholderUrlStrings:(nullable NSArray *)holderURLStrings atIndex:(NSInteger)index{ return [self showFromImageView:imageView withURLStrings:URLStrings placeholderURLStrings:holderURLStrings atIndex:index dismiss:nil]; } + (instancetype)showFromImageView:(UIImageView *)imageView withImages:(NSArray *)images atIndex:(NSInteger)index { return [self showFromImageView:imageView withImages:images atIndex:index dismiss:nil]; } + (nonnull instancetype)Type444showFromImageView:(nullable UIImageView *)imageView withImages:(nullable NSArray *)images atIndex:(NSInteger)index{ return [self Type444showFromImageView:imageView withImages:images atIndex:index dismiss:nil]; } - (instancetype)initWithFrame:(CGRect)frame Type:(HUPhotoBrowserType)type{ self = [super initWithFrame:frame]; if (self) { self.type = type; self.backgroundColor = [UIColor blackColor]; [self addSubview:self.collectionView]; [self setupToolBar]; [[NSNotificationCenter defaultCenter] removeObserver:self]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadForScreenRotate) name:UIDeviceOrientationDidChangeNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(photoCellDidZooming:) name:kPhotoCellDidZommingNotification object:nil]; } return self; } #pragma mark - UICollectionViewDataSource - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { NSInteger count = 0; if (self.URLStrings) { count = _URLStrings.count; } else if (self.images) { count = _images.count; } return count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { HUPhotoBrowserCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kPhotoBrowserCellID forIndexPath:indexPath]; cell.indexPath = indexPath; [cell resetZoomingScale]; __weak __typeof(self) wself = self; cell.tapActionBlock = ^(UITapGestureRecognizer *sender) { [wself dismiss]; }; if (self.URLStrings) { NSURL *url = [NSURL URLWithString:self.URLStrings[indexPath.row]]; if (indexPath.row != _index) { if (self.placeHolderUrlStrings) { UIImageView* temp = [[UIImageView alloc]init]; [temp sd_setImageWithURL:[NSURL URLWithString:self.placeHolderUrlStrings[indexPath.row]] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { if (image) { _placeholderImage = image; [cell.imageView sd_setImageWithURL:url placeholderImage:_placeholderImage]; } }]; }else{ [cell.imageView sd_setImageWithURL:url placeholderImage:_placeholderImage]; } } else { UIImage *placeHolder = _tmpImageView.image; [cell.imageView sd_setImageWithURL:url placeholderImage:placeHolder completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { if (!_imageDidLoaded) { _imageDidLoaded = YES; if (_animationCompleted) { self.collectionView.hidden = NO; [_tmpImageView removeFromSuperview]; _animationCompleted = NO; } } }]; } } else if (self.images) { cell.imageView.image = self.images[indexPath.row]; } return cell; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return kScreenRect.size; } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { _currentPage = scrollView.contentOffset.x/kScreenWidth + 0.5; _countLab.text = [NSString stringWithFormat:@"%zd/%zd",_currentPage+1,_imagesCount]; if (_zoomingIndexPath) { [self.collectionView reloadItemsAtIndexPaths:@[_zoomingIndexPath]]; _zoomingIndexPath = nil; } } #pragma mark - notification handler - (void)reloadForScreenRotate { _collectionView.frame = kScreenRect; [self.collectionView reloadData]; self.collectionView.contentOffset = CGPointMake(kScreenWidth * _currentPage,0); } - (void)photoCellDidZooming:(NSNotification *)nofit { NSIndexPath *indexPath = nofit.object; _zoomingIndexPath = indexPath; } #pragma mark - getter - (UICollectionView *)collectionView { if (!_collectionView) { UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; layout.minimumInteritemSpacing = 0; layout.minimumLineSpacing = 0; _collectionView = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:layout]; _collectionView.hidden = YES; _collectionView.pagingEnabled = YES; _collectionView.showsHorizontalScrollIndicator = NO; } return _collectionView; } #pragma mark - private - (void)configureBrowser { self.collectionView.delegate = self; self.collectionView.dataSource = self; [self.collectionView registerClass:[HUPhotoBrowserCell class] forCellWithReuseIdentifier:kPhotoBrowserCellID]; [[UIApplication sharedApplication].keyWindow addSubview:self]; } - (void)setupToolBar { _toolBar = [[UIView alloc] initWithFrame:CGRectMake(0, 28, self.frame.size.width, 30)]; _toolBar.backgroundColor = [UIColor clearColor]; [self addSubview:_toolBar]; UILabel *countLab = [[UILabel alloc] init]; countLab.textColor = [UIColor whiteColor]; countLab.layer.shadowOpacity = 0.9; countLab.layer.shadowColor = [UIColor blackColor].CGColor; countLab.layer.shadowOffset = CGSizeMake(1.0, 1.0); countLab.layer.shadowRadius= 2.0; countLab.font = [UIFont systemFontOfSize:15]; countLab.textAlignment = NSTextAlignmentCenter; [_toolBar addSubview:countLab]; _countLab = countLab; if (self.type==NormalType) { UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeCustom]; saveBtn.frame = CGRectMake(_toolBar.frame.size.width-58, 1, 50, 28); saveBtn.layer.shadowOpacity = 0.9; saveBtn.layer.shadowColor = [UIColor blackColor].CGColor; saveBtn.layer.shadowOffset = CGSizeMake(1.0, 1.0); saveBtn.layer.shadowRadius= 2.0; [saveBtn setTitle:@"保存" forState:UIControlStateNormal]; [saveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; saveBtn.titleLabel.font = [UIFont systemFontOfSize:15]; [saveBtn addTarget:self action:@selector(saveImae) forControlEvents:UIControlEventTouchUpInside]; // [JWUtils setShadow:saveBtn.layer]; // [_toolBar addSubview:saveBtn]; }else if(self.type==SpecialType ||self.type==Type333333){ // UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeCustom]; // saveBtn.frame = CGRectMake(_toolBar.frame.size.width-58, -5, 52, 44); // saveBtn.layer.shadowOpacity = 0.9; // saveBtn.layer.shadowColor = [UIColor blackColor].CGColor; // saveBtn.layer.shadowOffset = CGSizeMake(1.0, 1.0); // saveBtn.layer.shadowRadius= 2.0; //// [saveBtn setImage:[UIImage imageNamed:@"more_icon"] forState:UIControlStateNormal]; // [saveBtn setTitle:@"更多" forState:UIControlStateNormal]; // saveBtn.titleLabel.font = [UIFont systemFontOfSize:14]; // [saveBtn addTarget:self action:@selector(AlertShow) forControlEvents:UIControlEventTouchUpInside]; //// [JWUtils setShadow:saveBtn.layer]; // [_toolBar addSubview:saveBtn]; UIButton *deleteBt = [UIButton buttonWithType:UIButtonTypeCustom]; deleteBt.frame = CGRectMake(_toolBar.frame.size.width-80, KScreenHeight-50, 80, 50); deleteBt.layer.shadowOpacity = 0.9; deleteBt.layer.shadowColor = [UIColor blackColor].CGColor; deleteBt.layer.shadowOffset = CGSizeMake(1.0, 1.0); deleteBt.layer.shadowRadius= 2.0; [deleteBt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [deleteBt setTitle:@"删除" forState:UIControlStateNormal]; [deleteBt setContentMode:UIViewContentModeScaleAspectFit]; deleteBt.titleLabel.font = [UIFont systemFontOfSize:14]; [deleteBt addTarget:self action:@selector(deletePhoto) forControlEvents:UIControlEventTouchUpInside]; // [JWUtils setShadow:deleteBt.layer]; [self addSubview:deleteBt]; }else if(self.type==Type44444){ } } - (void)deletePhoto{ if (self.images.count==1) { if (self.delegate&&[self.delegate respondsToSelector:@selector(didDeletePhotoInIndex:)]) { [self.delegate didDeletePhotoInIndex:_currentPage]; } [self dismiss]; }else{ self.newImages = [NSMutableArray arrayWithArray:self.images]; [self.newImages removeObjectAtIndex:_currentPage]; // self.imageView = self.tmpImageView; self.images = self.newImages; self.imagesCount = self.newImages.count; if (self.delegate&&[self.delegate respondsToSelector:@selector(didDeletePhotoInIndex:)]) { [self.delegate didDeletePhotoInIndex:_currentPage]; } _currentPage = _index; if (_currentPage+1>self.imagesCount) { _currentPage = 0; } [self resetCountLabWithIndex:_currentPage+1]; // [self animateImageViewAtIndex:_currentPage]; [self.collectionView reloadData]; [self.collectionView setContentOffset:CGPointMake(KScreenWidth*_currentPage, 0) animated:NO]; } } - (void)animateImageViewAtIndex:(NSInteger)index { _index = index; CGRect startFrame = [self.imageView.superview convertRect:self.imageView.frame toView:[UIApplication sharedApplication].keyWindow]; CGRect endFrame = kScreenRect; if (self.imageView.image) { UIImage *image = self.imageView.image; CGFloat ratio = image.size.width / image.size.height; if (ratio > kScreenRatio) { endFrame.size.width = kScreenWidth; endFrame.size.height = kScreenWidth / ratio; } else { endFrame.size.height = kScreenHeight; endFrame.size.width = kScreenHeight * ratio; } endFrame.origin.x = (kScreenWidth - endFrame.size.width) / 2; endFrame.origin.y = (kScreenHeight - endFrame.size.height) / 2; } _endTempFrame = endFrame; #if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_9_0 [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade]; #endif UIImageView *tempImageView = [[UIImageView alloc] initWithFrame:startFrame]; tempImageView.image = self.imageView.image; tempImageView.contentMode = UIViewContentModeScaleAspectFit; [[UIApplication sharedApplication].keyWindow addSubview:tempImageView]; _tmpImageView = tempImageView; if (self.URLStrings && !self.images) { // NSString *key = [SDWebImageDownloader cacheKeyForURL:[NSURL URLWithString:self.URLStrings[_index]]]; // UIImage *image = [HUWebImageDownloader imageFromDiskCacheForKey:key]; // _imageDidLoaded = image != nil; } [self.collectionView setContentOffset:CGPointMake(kScreenWidth * index,0) animated:NO]; [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{ tempImageView.frame = endFrame; } completion:^(BOOL finished) { _currentPage = index; _animationCompleted = YES; if (self.images || _imageDidLoaded || (self.URLStrings && !_imageDidLoaded)) { self.collectionView.hidden = NO; [tempImageView removeFromSuperview]; _animationCompleted = NO; } }]; } - (void)dismiss { #if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_9_0 [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade]; #endif if (self.dismissDlock) { HUPhotoBrowserCell *cell = (HUPhotoBrowserCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:_currentPage inSection:0]]; self.dismissDlock(cell.imageView.image, _currentPage); } if (_currentPage != _index) { [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{ self.alpha = 0; } completion:^(BOOL finished) { [self removeFromSuperview]; }]; return; } CGRect endFrame = [self.imageView.superview convertRect:self.imageView.frame toView:[UIApplication sharedApplication].keyWindow]; UIImageView *tempImageView = [[UIImageView alloc] initWithFrame:_endTempFrame]; tempImageView.image = self.imageView.image; tempImageView.contentMode = UIViewContentModeScaleAspectFit; self.collectionView.hidden = YES; [[UIApplication sharedApplication].keyWindow addSubview:tempImageView]; [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{ tempImageView.frame = endFrame; self.alpha = 0; } completion:^(BOOL finished) { [self removeFromSuperview]; [tempImageView removeFromSuperview]; }]; } - (void)resetCountLabWithIndex:(NSInteger)index { NSString *text = [NSString stringWithFormat:@"%zd%zd",_imagesCount,_imagesCount]; CGFloat width = [text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]}].width+8; _countLab.frame = CGRectMake(8, 1, MAX(50, width), 28); _countLab.text = [NSString stringWithFormat:@"%zd/%zd",index,_imagesCount]; } - (void)saveImae { HUPhotoBrowserCell *cell = (HUPhotoBrowserCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:_currentPage inSection:0]]; UIImage *seavedImage = cell.imageView.image; UIImageWriteToSavedPhotosAlbum(seavedImage, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil); } - (void)setType:(HUPhotoBrowserType)type{ _type = type; // if (type == Type333333 || type==Type44444) { // [_toolBar setHidden:NO]; // } } @end