From f2decb182ded266eab10f97b5727e79bd74d7be8 Mon Sep 17 00:00:00 2001 From: longitachi Date: Wed, 28 Jun 2017 15:59:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=963dtouch=E9=80=A0=E6=88=90?= =?UTF-8?q?=E7=9A=84=E5=86=85=E5=AD=98=E6=B3=84=E6=BC=8F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.优化3dtouch造成的内存泄漏问题 2.取出视频预览字节显示 3.优化图片加载显示方式 --- PhotoBrowser/ZLCollectionCell.m | 61 ++++++++++++++---------- PhotoBrowser/ZLPhotoActionSheet.h | 2 +- PhotoBrowser/ZLPhotoManager.m | 2 +- PhotoBrowser/ZLShowVideoViewController.m | 10 ---- PhotoBrowser/ZLThumbnailViewController.m | 29 +++++------ README.md | 1 + ZLPhotoBrowser.podspec | 2 +- 7 files changed, 56 insertions(+), 51 deletions(-) diff --git a/PhotoBrowser/ZLCollectionCell.m b/PhotoBrowser/ZLCollectionCell.m index c5f04116..73f94c2b 100644 --- a/PhotoBrowser/ZLCollectionCell.m +++ b/PhotoBrowser/ZLCollectionCell.m @@ -12,6 +12,13 @@ #import "ZLDefine.h" #import "ToastUtils.h" +@interface ZLCollectionCell () + +@property (nonatomic, copy) NSString *identifier; +@property (nonatomic, assign) PHImageRequestID imageRequestID; + +@end + @implementation ZLCollectionCell - (void)awakeFromNib { @@ -19,26 +26,17 @@ - (void)awakeFromNib { [super awakeFromNib]; } -- (void)layoutSubviews -{ - [super layoutSubviews]; - self.imageView.frame = self.bounds; - self.btnSelect.frame = CGRectMake(GetViewWidth(self.contentView)-26, 5, 23, 23); -// self.topView.frame = self.bounds; - self.videoBottomView.frame = CGRectMake(0, GetViewHeight(self)-15, GetViewWidth(self), 15); - self.videoImageView.frame = CGRectMake(5, 1, 16, 12); - self.liveImageView.frame = CGRectMake(5, -1, 15, 15); - self.timeLabel.frame = CGRectMake(30, 1, GetViewWidth(self)-35, 12); - [self.contentView sendSubviewToBack:self.imageView]; -} - - (UIImageView *)imageView { if (!_imageView) { _imageView = [[UIImageView alloc] init]; + _imageView.frame = self.bounds; _imageView.contentMode = UIViewContentModeScaleAspectFill; _imageView.clipsToBounds = YES; [self.contentView addSubview:_imageView]; + + [self.contentView bringSubviewToFront:self.videoBottomView]; + [self.contentView bringSubviewToFront:self.btnSelect]; } return _imageView; } @@ -47,6 +45,7 @@ - (UIButton *)btnSelect { if (!_btnSelect) { _btnSelect = [UIButton buttonWithType:UIButtonTypeCustom]; + _btnSelect.frame = CGRectMake(GetViewWidth(self.contentView)-26, 5, 23, 23); [_btnSelect setBackgroundImage:GetImageWithName(@"btn_unselected.png") forState:UIControlStateNormal]; [_btnSelect setBackgroundImage:GetImageWithName(@"btn_selected.png") forState:UIControlStateSelected]; [_btnSelect addTarget:self action:@selector(btnSelectClick:) forControlEvents:UIControlEventTouchUpInside]; @@ -59,9 +58,7 @@ - (UIImageView *)videoBottomView { if (!_videoBottomView) { _videoBottomView = [[UIImageView alloc] initWithImage:GetImageWithName(@"videoView")]; - [_videoBottomView addSubview:self.videoImageView]; - [_videoBottomView addSubview:self.liveImageView]; - [_videoBottomView addSubview:self.timeLabel]; + _videoBottomView.frame = CGRectMake(0, GetViewHeight(self)-15, GetViewWidth(self), 15); [self.contentView addSubview:_videoBottomView]; } return _videoBottomView; @@ -70,8 +67,9 @@ - (UIImageView *)videoBottomView - (UIImageView *)videoImageView { if (!_videoImageView) { - _videoImageView = [[UIImageView alloc] init]; + _videoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 1, 16, 12)]; _videoImageView.image = GetImageWithName(@"video"); + [self.videoBottomView addSubview:_videoImageView]; } return _videoImageView; } @@ -79,8 +77,9 @@ - (UIImageView *)videoImageView - (UIImageView *)liveImageView { if (!_liveImageView) { - _liveImageView = [[UIImageView alloc] init]; + _liveImageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, -1, 15, 15)]; _liveImageView.image = GetImageWithName(@"livePhoto"); + [self.videoBottomView addSubview:_liveImageView]; } return _liveImageView; } @@ -88,10 +87,11 @@ - (UIImageView *)liveImageView - (UILabel *)timeLabel { if (!_timeLabel) { - _timeLabel = [[UILabel alloc] init]; + _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 1, GetViewWidth(self)-35, 12)]; _timeLabel.textAlignment = NSTextAlignmentRight; _timeLabel.font = [UIFont systemFontOfSize:13]; _timeLabel.textColor = [UIColor whiteColor]; + [self.videoBottomView addSubview:_timeLabel]; } return _timeLabel; } @@ -153,15 +153,28 @@ - (void)setModel:(ZLPhotoModel *)model } self.btnSelect.selected = model.isSelected; - + CGSize size; - size.width = GetViewWidth(self) * 2.5; - size.height = GetViewHeight(self) * 2.5; + size.width = GetViewWidth(self) * 1.7; + size.height = GetViewHeight(self) * 1.7; weakify(self); - [ZLPhotoManager requestImageForAsset:model.asset size:size completion:^(UIImage *image, NSDictionary *info) { + if (model.asset && self.imageRequestID >= 0) { + NSLog(@"cancel"); + [[PHCachingImageManager defaultManager] cancelImageRequest:self.imageRequestID]; + } + self.identifier = model.asset.localIdentifier; + self.imageView.image = nil; + self.imageRequestID = [ZLPhotoManager requestImageForAsset:model.asset size:size completion:^(UIImage *image, NSDictionary *info) { strongify(weakSelf); - strongSelf.imageView.image = image; + + if ([strongSelf.identifier isEqualToString:model.asset.localIdentifier]) { + strongSelf.imageView.image = image; + } + + if (![[info objectForKey:PHImageResultIsDegradedKey] boolValue]) { + strongSelf.imageRequestID = -1; + } }]; } diff --git a/PhotoBrowser/ZLPhotoActionSheet.h b/PhotoBrowser/ZLPhotoActionSheet.h index 33565163..2e894c0c 100644 --- a/PhotoBrowser/ZLPhotoActionSheet.h +++ b/PhotoBrowser/ZLPhotoActionSheet.h @@ -72,7 +72,7 @@ NS_ASSUME_NONNULL_BEGIN /**选择live photo照片回调,回调解析好的live photo图片、对应的asset对象*/ @property (nonatomic, copy) void (^selectLivePhotoBlock)(UIImage *livePhoto, PHAsset *asset); -/**选择视频回调,回调第一帧封面图片、对应的asset对象,对应的AVPlayerItem对象*/ +/**选择视频回调,回调第一帧封面图片、对应的asset对象*/ @property (nonatomic, copy) void (^selectVideoBlock)(UIImage *cover, PHAsset *asset); - (instancetype)initWithFrame:(CGRect)frame NS_UNAVAILABLE; diff --git a/PhotoBrowser/ZLPhotoManager.m b/PhotoBrowser/ZLPhotoManager.m index 657b0c84..14090f16 100644 --- a/PhotoBrowser/ZLPhotoManager.m +++ b/PhotoBrowser/ZLPhotoManager.m @@ -364,7 +364,7 @@ + (PHImageRequestID)requestImageForAsset:(PHAsset *)asset size:(CGSize)size resi PHImageErrorKey:如果没有图像,字典内的错误信息 */ - return [[PHCachingImageManager defaultManager] requestImageForAsset:asset targetSize:size contentMode:PHImageContentModeAspectFit options:option resultHandler:^(UIImage * _Nullable image, NSDictionary * _Nullable info) { + return [[PHCachingImageManager defaultManager] requestImageForAsset:asset targetSize:size contentMode:PHImageContentModeAspectFill options:option resultHandler:^(UIImage * _Nullable image, NSDictionary * _Nullable info) { BOOL downloadFinined = ![[info objectForKey:PHImageCancelledKey] boolValue] && ![info objectForKey:PHImageErrorKey]; //不要该判断,即如果该图片在iCloud上时候,会先显示一张模糊的预览图,待加载完毕后会显示高清图 // && ![[info objectForKey:PHImageResultIsDegradedKey] boolValue] diff --git a/PhotoBrowser/ZLShowVideoViewController.m b/PhotoBrowser/ZLShowVideoViewController.m index f21cccbf..879f1fa8 100644 --- a/PhotoBrowser/ZLShowVideoViewController.m +++ b/PhotoBrowser/ZLShowVideoViewController.m @@ -19,7 +19,6 @@ @interface ZLShowVideoViewController () @property (nonatomic, strong) UIImage *coverImage; @property (nonatomic, strong) UIButton *playBtn; @property (nonatomic, strong) UIView *bottomView; -@property (nonatomic, strong) UILabel *labVideoBytes; @property (nonatomic, strong) UIButton *btnDone; @property (nonatomic, strong) UILabel *icloudLoadFailedLabel; @@ -101,11 +100,6 @@ - (void)initUI [self.view addSubview:_bottomView]; - self.labVideoBytes = [[UILabel alloc] initWithFrame:CGRectMake(12, 7, 80, 30)]; - self.labVideoBytes.font = [UIFont systemFontOfSize:15]; - self.labVideoBytes.textColor = kDoneButton_bgColor; - [self.bottomView addSubview:self.labVideoBytes]; - self.btnDone = [UIButton buttonWithType:UIButtonTypeCustom]; self.btnDone.frame = CGRectMake(kViewWidth - 82, 7, 70, 30); [self.btnDone setTitle:GetLocalLanguageTextValue(ZLPhotoBrowserDoneText) forState:UIControlStateNormal]; @@ -128,10 +122,6 @@ - (void)initUI [self.view addGestureRecognizer:tap]; weakify(self); - [ZLPhotoManager getPhotosBytesWithArray:@[self.model] completion:^(NSString *photosBytes) { - strongify(weakSelf); - strongSelf.labVideoBytes.text = photosBytes; - }]; [ZLPhotoManager requestOriginalImageForAsset:self.model.asset completion:^(UIImage *image, NSDictionary *info) { if ([[info objectForKey:PHImageResultIsDegradedKey] boolValue]) return; strongify(weakSelf); diff --git a/PhotoBrowser/ZLThumbnailViewController.m b/PhotoBrowser/ZLThumbnailViewController.m index 872384bb..bca5cbdd 100644 --- a/PhotoBrowser/ZLThumbnailViewController.m +++ b/PhotoBrowser/ZLThumbnailViewController.m @@ -170,6 +170,11 @@ - (void)initCollectionView [self.collectionView registerClass:NSClassFromString(@"ZLTakePhotoCell") forCellWithReuseIdentifier:@"ZLTakePhotoCell"]; [self.collectionView registerClass:NSClassFromString(@"ZLCollectionCell") forCellWithReuseIdentifier:@"ZLCollectionCell"]; + //注册3d touch + ZLImageNavigationController *nav = (ZLImageNavigationController *)self.navigationController; + if (nav.allowForceTouch && [self forceTouchAvailable]) { + [self registerForPreviewingWithDelegate:self sourceView:self.collectionView]; + } } - (void)initNavBtn @@ -287,8 +292,7 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell } else { model = self.arrDataSources[indexPath.row-1]; } - cell.imageView.contentMode = UIViewContentModeScaleAspectFill; - + weakify(self); __weak typeof(cell) weakCell = cell; @@ -323,22 +327,17 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell // [collectionView reloadItemsAtIndexPaths:[collectionView indexPathsForVisibleItems]]; [strongSelf resetBottomBtnsStatus]; }; -// cell.isSelectedImage = ^BOOL() { -// strongify(weakSelf); -// ZLImageNavigationController *nav = (ZLImageNavigationController *)strongSelf.navigationController; -// return nav.arrSelectedModels.count > 0; -// }; +//// cell.isSelectedImage = ^BOOL() { +//// strongify(weakSelf); +//// ZLImageNavigationController *nav = (ZLImageNavigationController *)strongSelf.navigationController; +//// return nav.arrSelectedModels.count > 0; +//// }; cell.allSelectGif = nav.allowSelectGif; cell.allSelectLivePhoto = nav.allowSelectLivePhoto; cell.showSelectBtn = nav.showSelectBtn; cell.cornerRadio = nav.cellCornerRadio; cell.model = model; - - //3d touch - if (nav.allowForceTouch && [self forceTouchAvailable]) { - [self registerForPreviewingWithDelegate:self sourceView:cell]; - } - + return cell; } @@ -517,11 +516,13 @@ - (void)getOriginalImageBytes //!!!!: 3D Touch - (UIViewController *)previewingContext:(id)previewingContext viewControllerForLocation:(CGPoint)location { - NSIndexPath *indexPath = [self.collectionView indexPathForCell:(UICollectionViewCell *)previewingContext.sourceView]; + NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:location]; if (!indexPath) { return nil; } + //设置突出区域 + previewingContext.sourceRect = [self.collectionView cellForItemAtIndexPath:indexPath].frame; ZLForceTouchPreviewController *vc = [[ZLForceTouchPreviewController alloc] init]; diff --git a/README.md b/README.md index 617acb5b..cf9f1d4b 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ ### 更新日志 ``` +● 2.2.3: 新增图片编辑功能; ● 2.2.1: 新增3D Touch预览功能 (需设备支持); ● 2.2.0: 优化内存问题; ● 2.1.9: 新增选择及预览Live Photo功能 (iOS 9.0); diff --git a/ZLPhotoBrowser.podspec b/ZLPhotoBrowser.podspec index 07438672..ef6d4c0f 100644 --- a/ZLPhotoBrowser.podspec +++ b/ZLPhotoBrowser.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'ZLPhotoBrowser' - s.version = '2.2.3' + s.version = '2.2.4' s.summary = 'An easy way to Multiselect photos,video,gif,livephoto from ablum, force touch to preview image, and support for multiple languages (Chinese, English, Japanese ...)' s.homepage = 'https://github.com/longitachi/ZLPhotoBrowser' s.license = 'MIT'