Skip to content

Commit

Permalink
优化3dtouch造成的内存泄漏问题
Browse files Browse the repository at this point in the history
1.优化3dtouch造成的内存泄漏问题
2.取出视频预览字节显示
3.优化图片加载显示方式
  • Loading branch information
longitachi committed Jun 28, 2017
1 parent 081a215 commit f2decb1
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 51 deletions.
61 changes: 37 additions & 24 deletions PhotoBrowser/ZLCollectionCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,31 @@
#import "ZLDefine.h"
#import "ToastUtils.h"

@interface ZLCollectionCell ()

@property (nonatomic, copy) NSString *identifier;
@property (nonatomic, assign) PHImageRequestID imageRequestID;

@end

@implementation ZLCollectionCell

- (void)awakeFromNib {
// Initialization code
[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;
}
Expand All @@ -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];
Expand All @@ -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;
Expand All @@ -70,28 +67,31 @@ - (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;
}

- (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;
}

- (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;
}
Expand Down Expand Up @@ -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;
}
}];
}

Expand Down
2 changes: 1 addition & 1 deletion PhotoBrowser/ZLPhotoActionSheet.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion PhotoBrowser/ZLPhotoManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
10 changes: 0 additions & 10 deletions PhotoBrowser/ZLShowVideoViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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];
Expand All @@ -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);
Expand Down
29 changes: 15 additions & 14 deletions PhotoBrowser/ZLThumbnailViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -517,11 +516,13 @@ - (void)getOriginalImageBytes
//!!!!: 3D Touch
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)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];

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

### 更新日志
```
● 2.2.3: 新增图片编辑功能;
● 2.2.1: 新增3D Touch预览功能 (需设备支持);
● 2.2.0: 优化内存问题;
● 2.1.9: 新增选择及预览Live Photo功能 (iOS 9.0);
Expand Down
2 changes: 1 addition & 1 deletion ZLPhotoBrowser.podspec
Original file line number Diff line number Diff line change
@@ -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'
Expand Down

0 comments on commit f2decb1

Please sign in to comment.