Skip to content

Commit

Permalink
提供逐个解析api 及 控制是否允许选择原图参数
Browse files Browse the repository at this point in the history
  • Loading branch information
longitachi committed Oct 26, 2017
1 parent b33ab91 commit 6579bd2
Show file tree
Hide file tree
Showing 12 changed files with 193 additions and 71 deletions.
7 changes: 6 additions & 1 deletion PhotoBrowser/ZLPhotoActionSheet.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Created by long on 15/11/25.
// Copyright © 2015年 long. All rights reserved.
//
//pods version 2.5.0 - 2017.10.23 update
//pods version 2.5.0.1 - 2017.10.26 update

#import <UIKit/UIKit.h>
#import "ZLDefine.h"
Expand Down Expand Up @@ -81,6 +81,11 @@ NS_ASSUME_NONNULL_BEGIN
*/
@property (nonatomic, assign) BOOL allowEditVideo;

/**
是否允许选择原图,默认YES
*/
@property (nonatomic, assign) BOOL allowSelectOriginal;

/**
编辑视频时最大裁剪时间,单位:秒,默认10s 且最低10s
Expand Down
33 changes: 3 additions & 30 deletions PhotoBrowser/ZLPhotoActionSheet.m
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ - (instancetype)init
self.allowForceTouch = YES;
self.allowEditImage = YES;
self.allowEditVideo = NO;
self.allowSelectOriginal = YES;
self.maxEditVideoTime = 10;
self.allowSlideSelect = YES;
self.allowDragSelect = NO;
Expand Down Expand Up @@ -585,7 +586,7 @@ - (void)requestSelPhotos:(UIViewController *)vc data:(NSArray<ZLPhotoModel *> *)

zl_strongify(weakSelf);
if (image) {
[photos replaceObjectAtIndex:i withObject:[strongSelf scaleImage:image]];
[photos replaceObjectAtIndex:i withObject:[ZLPhotoManager scaleImage:image original:strongSelf->_isSelectOriginalPhoto]];
[assets replaceObjectAtIndex:i withObject:model.asset];
}

Expand All @@ -607,35 +608,6 @@ - (void)requestSelPhotos:(UIViewController *)vc data:(NSArray<ZLPhotoModel *> *)
}
}

/**
* @brief 这里对拿到的图片进行缩放,不然原图直接返回的话会造成内存暴涨
*/
- (UIImage *)scaleImage:(UIImage *)image
{
NSData *data = UIImageJPEGRepresentation(image, 1);

if (data.length < 0.2*(1024*1024)) {
//小于200k不缩放
return image;
}

double scale = _isSelectOriginalPhoto ? (data.length>(1024*1024)?.7:.9) : (data.length>(1024*1024)?.5:.7);
NSData *d = UIImageJPEGRepresentation(image, scale);

return [UIImage imageWithData:d];

// CGSize size = CGSizeMake(ScalePhotoWidth, ScalePhotoWidth * image.size.height / image.size.width);
// if (image.size.width < size.width
// ) {
// return image;
// }
// UIGraphicsBeginImageContext(size);
// [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
// UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
// UIGraphicsEndImageContext();
// return newImage;
}

#pragma mark - UICollectionDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
Expand Down Expand Up @@ -833,6 +805,7 @@ - (ZLImageNavigationController *)getImageNavWithRootVC:(UIViewController *)rootV
nav.allowForceTouch = self.allowForceTouch;
nav.allowEditImage = self.allowEditImage;
nav.allowEditVideo = self.allowEditVideo;
nav.allowSelectOriginal = self.allowSelectOriginal;
nav.maxEditVideoTime = self.maxEditVideoTime;
nav.allowSlideSelect = self.allowSlideSelect;
nav.editAfterSelectThumbnailImage = self.editAfterSelectThumbnailImage;
Expand Down
5 changes: 5 additions & 0 deletions PhotoBrowser/ZLPhotoBrowser.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@
*/
@property (nonatomic, assign) BOOL allowEditVideo;

/**
是否允许选择原图,默认YES
*/
@property (nonatomic, assign) BOOL allowSelectOriginal;

/**
编辑视频时最大裁剪时间,单位:秒,默认10s
Expand Down
17 changes: 16 additions & 1 deletion PhotoBrowser/ZLPhotoManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,22 @@
*/
+ (void)requestVideoForAsset:(PHAsset *)asset completion:(void (^)(AVPlayerItem *item, NSDictionary *info))completion;

#pragma mark - 逐个解析asset方法
/**
自行解析图片方法
使用顺序单个解析,缓解了框架同时解析大量图片造成的内存暴涨
如果一下选择20张及以上照片(原图)建议使用自行解析
请求到图片后做了一个大小的压缩(原图时并未压缩尺寸)来缓解内存的占用
*/
+ (void)anialysisAssets:(NSArray<PHAsset *> *)assets original:(BOOL)original completion:(void (^)(NSArray<UIImage *> *images))completion;

/**
@brief 缩放图片
*/
+ (UIImage *)scaleImage:(UIImage *)image original:(BOOL)original;

/**
* @brief 将系统mediatype转换为自定义mediatype
*/
Expand All @@ -110,7 +126,6 @@
*/
+ (UIImage *)transformToGifImageWithData:(NSData *)data;


#pragma mark - 编辑视频相关

/**
Expand Down
74 changes: 74 additions & 0 deletions PhotoBrowser/ZLPhotoManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,80 @@ + (void)requestVideoForAsset:(PHAsset *)asset completion:(void (^)(AVPlayerItem
}];
}

+ (void)anialysisAssets:(NSArray<PHAsset *> *)assets original:(BOOL)original completion:(void (^)(NSArray<UIImage *> *))completion
{
NSMutableArray *arr = [NSMutableArray array];

dispatch_queue_t queue = dispatch_queue_create(nil, 0);

dispatch_semaphore_t sem = dispatch_semaphore_create(1);

for (int i = 0; i < assets.count; i++) {
PHAsset *asset = assets[i];

dispatch_async(queue, ^{
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
zl_weakify(self);
if (original) {
[self requestOriginalImageForAsset:asset completion:^(UIImage *image, NSDictionary *info) {
if ([[info objectForKey:PHImageResultIsDegradedKey] boolValue]) return;
dispatch_semaphore_signal(sem);
zl_strongify(weakSelf);

[arr addObject:[strongSelf scaleImage:image original:original]];
if (i == assets.count-1) {
dispatch_async(dispatch_get_main_queue(), ^{
if (completion) completion(arr);
});
}
}];
} else {
CGFloat scale = 2;
CGFloat width = MIN(kViewWidth, kMaxImageWidth);
CGSize size = CGSizeMake(width*scale, width*scale*asset.pixelHeight/asset.pixelWidth);
[self requestImageForAsset:asset size:size completion:^(UIImage *image, NSDictionary *info) {
if ([[info objectForKey:PHImageResultIsDegradedKey] boolValue]) return;
dispatch_semaphore_signal(sem);
zl_strongify(weakSelf);

[arr addObject:[strongSelf scaleImage:image original:original]];
if (i == assets.count-1) {
dispatch_async(dispatch_get_main_queue(), ^{
if (completion) completion(arr);
});
}
}];
}
});
}
}

+ (UIImage *)scaleImage:(UIImage *)image original:(BOOL)original
{
NSData *data = UIImageJPEGRepresentation(image, 1);

if (data.length < 0.2*(1024*1024)) {
//小于200k不缩放
return image;
}

double scale = original ? (data.length>(1024*1024)?.7:.9) : (data.length>(1024*1024)?.5:.7);
NSData *d = UIImageJPEGRepresentation(image, scale);

return [UIImage imageWithData:d];

// CGSize size = CGSizeMake(ScalePhotoWidth, ScalePhotoWidth * image.size.height / image.size.width);
// if (image.size.width < size.width
// ) {
// return image;
// }
// UIGraphicsBeginImageContext(size);
// [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
// UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
// UIGraphicsEndImageContext();
// return newImage;
}

#pragma mark - 获取asset对应的图片
+ (PHImageRequestID)requestImageForAsset:(PHAsset *)asset size:(CGSize)size resizeMode:(PHImageRequestOptionsResizeMode)resizeMode completion:(void (^)(UIImage *, NSDictionary *))completion
{
Expand Down
38 changes: 20 additions & 18 deletions PhotoBrowser/ZLShowBigImgViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -240,24 +240,26 @@ - (void)initBottomView
_bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, kViewHeight - 44, kViewWidth, 44)];
_bottomView.backgroundColor = nav.bottomViewBgColor;

_btnOriginalPhoto = [UIButton buttonWithType:UIButtonTypeCustom];
[_btnOriginalPhoto setTitle:GetLocalLanguageTextValue(ZLPhotoBrowserOriginalText) forState:UIControlStateNormal];
_btnOriginalPhoto.titleLabel.font = [UIFont systemFontOfSize:15];
[_btnOriginalPhoto setTitleColor:nav.bottomBtnsNormalTitleColor forState: UIControlStateNormal];
UIImage *normalImg = GetImageWithName(@"btn_original_circle");
UIImage *selImg = GetImageWithName(@"btn_selected");
[_btnOriginalPhoto setImage:normalImg forState:UIControlStateNormal];
[_btnOriginalPhoto setImage:selImg forState:UIControlStateSelected];
[_btnOriginalPhoto setImageEdgeInsets:UIEdgeInsetsMake(0, -5, 0, 5)];
[_btnOriginalPhoto addTarget:self action:@selector(btnOriginalImage_Click:) forControlEvents:UIControlEventTouchUpInside];
_btnOriginalPhoto.selected = nav.isSelectOriginalPhoto;
[self getPhotosBytes];
[_bottomView addSubview:_btnOriginalPhoto];

self.labPhotosBytes = [[UILabel alloc] init];
self.labPhotosBytes.font = [UIFont systemFontOfSize:15];
self.labPhotosBytes.textColor = nav.bottomBtnsNormalTitleColor;
[_bottomView addSubview:self.labPhotosBytes];
if (nav.allowSelectOriginal) {
_btnOriginalPhoto = [UIButton buttonWithType:UIButtonTypeCustom];
[_btnOriginalPhoto setTitle:GetLocalLanguageTextValue(ZLPhotoBrowserOriginalText) forState:UIControlStateNormal];
_btnOriginalPhoto.titleLabel.font = [UIFont systemFontOfSize:15];
[_btnOriginalPhoto setTitleColor:nav.bottomBtnsNormalTitleColor forState: UIControlStateNormal];
UIImage *normalImg = GetImageWithName(@"btn_original_circle");
UIImage *selImg = GetImageWithName(@"btn_selected");
[_btnOriginalPhoto setImage:normalImg forState:UIControlStateNormal];
[_btnOriginalPhoto setImage:selImg forState:UIControlStateSelected];
[_btnOriginalPhoto setImageEdgeInsets:UIEdgeInsetsMake(0, -5, 0, 5)];
[_btnOriginalPhoto addTarget:self action:@selector(btnOriginalImage_Click:) forControlEvents:UIControlEventTouchUpInside];
_btnOriginalPhoto.selected = nav.isSelectOriginalPhoto;
[self getPhotosBytes];
[_bottomView addSubview:_btnOriginalPhoto];

self.labPhotosBytes = [[UILabel alloc] init];
self.labPhotosBytes.font = [UIFont systemFontOfSize:15];
self.labPhotosBytes.textColor = nav.bottomBtnsNormalTitleColor;
[_bottomView addSubview:self.labPhotosBytes];
}

//编辑
_btnEdit = [UIButton buttonWithType:UIButtonTypeCustom];
Expand Down
36 changes: 20 additions & 16 deletions PhotoBrowser/ZLThumbnailViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,12 @@ - (void)viewDidLayoutSubviews
self.btnPreView.frame = CGRectMake(offsetX, 7, GetMatchValue(GetLocalLanguageTextValue(ZLPhotoBrowserPreviewText), 15, YES, bottomBtnH), bottomBtnH);
offsetX = CGRectGetMaxX(self.btnPreView.frame) + 10;

self.btnOriginalPhoto.frame = CGRectMake(offsetX, 7, GetMatchValue(GetLocalLanguageTextValue(ZLPhotoBrowserOriginalText), 15, YES, bottomBtnH)+self.btnOriginalPhoto.imageView.frame.size.width, bottomBtnH);
offsetX = CGRectGetMaxX(self.btnOriginalPhoto.frame) + 5;

self.labPhotosBytes.frame = CGRectMake(offsetX, 7, 80, bottomBtnH);
if (nav.allowSelectOriginal) {
self.btnOriginalPhoto.frame = CGRectMake(offsetX, 7, GetMatchValue(GetLocalLanguageTextValue(ZLPhotoBrowserOriginalText), 15, YES, bottomBtnH)+self.btnOriginalPhoto.imageView.frame.size.width, bottomBtnH);
offsetX = CGRectGetMaxX(self.btnOriginalPhoto.frame) + 5;

self.labPhotosBytes.frame = CGRectMake(offsetX, 7, 80, bottomBtnH);
}

CGFloat doneWidth = GetMatchValue(self.btnDone.currentTitle, 15, YES, bottomBtnH);
doneWidth = MAX(70, doneWidth);
Expand Down Expand Up @@ -337,18 +339,20 @@ - (void)setupBottomView
[self.btnPreView addTarget:self action:@selector(btnPreview_Click:) forControlEvents:UIControlEventTouchUpInside];
[self.bottomView addSubview:self.btnPreView];

self.btnOriginalPhoto = [UIButton buttonWithType:UIButtonTypeCustom];
self.btnOriginalPhoto.titleLabel.font = [UIFont systemFontOfSize:15];
[self.btnOriginalPhoto setImage:GetImageWithName(@"btn_original_circle") forState:UIControlStateNormal];
[self.btnOriginalPhoto setImage:GetImageWithName(@"btn_selected") forState:UIControlStateSelected];
[self.btnOriginalPhoto setTitle:GetLocalLanguageTextValue(ZLPhotoBrowserOriginalText) forState:UIControlStateNormal];
[self.btnOriginalPhoto addTarget:self action:@selector(btnOriginalPhoto_Click:) forControlEvents:UIControlEventTouchUpInside];
[self.bottomView addSubview:self.btnOriginalPhoto];

self.labPhotosBytes = [[UILabel alloc] init];
self.labPhotosBytes.font = [UIFont systemFontOfSize:15];
self.labPhotosBytes.textColor = nav.bottomBtnsNormalTitleColor;
[self.bottomView addSubview:self.labPhotosBytes];
if (nav.allowSelectOriginal) {
self.btnOriginalPhoto = [UIButton buttonWithType:UIButtonTypeCustom];
self.btnOriginalPhoto.titleLabel.font = [UIFont systemFontOfSize:15];
[self.btnOriginalPhoto setImage:GetImageWithName(@"btn_original_circle") forState:UIControlStateNormal];
[self.btnOriginalPhoto setImage:GetImageWithName(@"btn_selected") forState:UIControlStateSelected];
[self.btnOriginalPhoto setTitle:GetLocalLanguageTextValue(ZLPhotoBrowserOriginalText) forState:UIControlStateNormal];
[self.btnOriginalPhoto addTarget:self action:@selector(btnOriginalPhoto_Click:) forControlEvents:UIControlEventTouchUpInside];
[self.bottomView addSubview:self.btnOriginalPhoto];

self.labPhotosBytes = [[UILabel alloc] init];
self.labPhotosBytes.font = [UIFont systemFontOfSize:15];
self.labPhotosBytes.textColor = nav.bottomBtnsNormalTitleColor;
[self.bottomView addSubview:self.labPhotosBytes];
}

self.btnDone = [UIButton buttonWithType:UIButtonTypeCustom];
self.btnDone.titleLabel.font = [UIFont systemFontOfSize:15];
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
### 更新日志
```
● 2.5.0.1: 提供逐个解析图片api,方便`shouldAnialysisAsset`为`NO`时的使用; 提供控制是否可以选择原图参数;
● 2.5.0: 新增选择后是否自动解析图片参数`shouldAnialysisAsset`(针对需要选择大量图片的功能,框架一次解析大量图片时,会导致内存瞬间大幅增高,建议此时置该参数为NO,然后拿到asset后自行逐个解析); 修改图片压缩方式,确保原图尺寸不变
● 2.4.9: 新增预览界面拖拽选择的功能; 支持开发者使用自定义图片资源; 开放导航标题颜色、底部工具栏背景色、底部按钮可交互与不可交互标题颜色的设置api;
● 2.4.6: 新增网络图片长按保存至相册功能;
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.5.0'
s.version = '2.5.0.1'
s.summary = 'A simple way to multiselect photos from ablum, force touch to preview photo, support portrait and landscape, edit photo, multiple languages(Chinese,English,Japanese)'
s.homepage = 'https://github.com/longitachi/ZLPhotoBrowser'
s.license = 'MIT'
Expand Down
Loading

0 comments on commit 6579bd2

Please sign in to comment.