diff --git a/PhotoBrowser/ZLPhotoActionSheet.h b/PhotoBrowser/ZLPhotoActionSheet.h index 789606f5..d1c48e77 100644 --- a/PhotoBrowser/ZLPhotoActionSheet.h +++ b/PhotoBrowser/ZLPhotoActionSheet.h @@ -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 #import "ZLDefine.h" @@ -81,6 +81,11 @@ NS_ASSUME_NONNULL_BEGIN */ @property (nonatomic, assign) BOOL allowEditVideo; +/** + 是否允许选择原图,默认YES + */ +@property (nonatomic, assign) BOOL allowSelectOriginal; + /** 编辑视频时最大裁剪时间,单位:秒,默认10s 且最低10s diff --git a/PhotoBrowser/ZLPhotoActionSheet.m b/PhotoBrowser/ZLPhotoActionSheet.m index dd8c507d..9cad9137 100644 --- a/PhotoBrowser/ZLPhotoActionSheet.m +++ b/PhotoBrowser/ZLPhotoActionSheet.m @@ -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; @@ -585,7 +586,7 @@ - (void)requestSelPhotos:(UIViewController *)vc data:(NSArray *) 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]; } @@ -607,35 +608,6 @@ - (void)requestSelPhotos:(UIViewController *)vc data:(NSArray *) } } -/** - * @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 { @@ -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; diff --git a/PhotoBrowser/ZLPhotoBrowser.h b/PhotoBrowser/ZLPhotoBrowser.h index 9cc193ef..413032eb 100644 --- a/PhotoBrowser/ZLPhotoBrowser.h +++ b/PhotoBrowser/ZLPhotoBrowser.h @@ -71,6 +71,11 @@ */ @property (nonatomic, assign) BOOL allowEditVideo; +/** + 是否允许选择原图,默认YES + */ +@property (nonatomic, assign) BOOL allowSelectOriginal; + /** 编辑视频时最大裁剪时间,单位:秒,默认10s diff --git a/PhotoBrowser/ZLPhotoManager.h b/PhotoBrowser/ZLPhotoManager.h index ed4919af..37d81aee 100644 --- a/PhotoBrowser/ZLPhotoManager.h +++ b/PhotoBrowser/ZLPhotoManager.h @@ -85,6 +85,22 @@ */ + (void)requestVideoForAsset:(PHAsset *)asset completion:(void (^)(AVPlayerItem *item, NSDictionary *info))completion; +#pragma mark - 逐个解析asset方法 +/** + 自行解析图片方法 + + 使用顺序单个解析,缓解了框架同时解析大量图片造成的内存暴涨 + 如果一下选择20张及以上照片(原图)建议使用自行解析 + + 请求到图片后做了一个大小的压缩(原图时并未压缩尺寸)来缓解内存的占用 + */ ++ (void)anialysisAssets:(NSArray *)assets original:(BOOL)original completion:(void (^)(NSArray *images))completion; + +/** + @brief 缩放图片 + */ ++ (UIImage *)scaleImage:(UIImage *)image original:(BOOL)original; + /** * @brief 将系统mediatype转换为自定义mediatype */ @@ -110,7 +126,6 @@ */ + (UIImage *)transformToGifImageWithData:(NSData *)data; - #pragma mark - 编辑视频相关 /** diff --git a/PhotoBrowser/ZLPhotoManager.m b/PhotoBrowser/ZLPhotoManager.m index ae8497c1..ca3c74af 100644 --- a/PhotoBrowser/ZLPhotoManager.m +++ b/PhotoBrowser/ZLPhotoManager.m @@ -387,6 +387,80 @@ + (void)requestVideoForAsset:(PHAsset *)asset completion:(void (^)(AVPlayerItem }]; } ++ (void)anialysisAssets:(NSArray *)assets original:(BOOL)original completion:(void (^)(NSArray *))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 { diff --git a/PhotoBrowser/ZLShowBigImgViewController.m b/PhotoBrowser/ZLShowBigImgViewController.m index 3d85a5e0..22334083 100644 --- a/PhotoBrowser/ZLShowBigImgViewController.m +++ b/PhotoBrowser/ZLShowBigImgViewController.m @@ -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]; diff --git a/PhotoBrowser/ZLThumbnailViewController.m b/PhotoBrowser/ZLThumbnailViewController.m index 03662ba7..62bdb4c8 100644 --- a/PhotoBrowser/ZLThumbnailViewController.m +++ b/PhotoBrowser/ZLThumbnailViewController.m @@ -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); @@ -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]; diff --git a/README.md b/README.md index 29e0b7b4..8a0d0d15 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,7 @@ ### 更新日志 ``` +● 2.5.0.1: 提供逐个解析图片api,方便`shouldAnialysisAsset`为`NO`时的使用; 提供控制是否可以选择原图参数; ● 2.5.0: 新增选择后是否自动解析图片参数`shouldAnialysisAsset`(针对需要选择大量图片的功能,框架一次解析大量图片时,会导致内存瞬间大幅增高,建议此时置该参数为NO,然后拿到asset后自行逐个解析); 修改图片压缩方式,确保原图尺寸不变 ● 2.4.9: 新增预览界面拖拽选择的功能; 支持开发者使用自定义图片资源; 开放导航标题颜色、底部工具栏背景色、底部按钮可交互与不可交互标题颜色的设置api; ● 2.4.6: 新增网络图片长按保存至相册功能; diff --git a/ZLPhotoBrowser.podspec b/ZLPhotoBrowser.podspec index 3710e915..63d8c311 100644 --- a/ZLPhotoBrowser.podspec +++ b/ZLPhotoBrowser.podspec @@ -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' diff --git a/ZLPhotoBrowser/Base.lproj/Main.storyboard b/ZLPhotoBrowser/Base.lproj/Main.storyboard index 61eb7b59..d47b4e75 100644 --- a/ZLPhotoBrowser/Base.lproj/Main.storyboard +++ b/ZLPhotoBrowser/Base.lproj/Main.storyboard @@ -328,14 +328,27 @@ - + + + + + @@ -381,6 +394,7 @@ + @@ -400,7 +414,7 @@ - + @@ -424,6 +438,7 @@ + @@ -433,11 +448,14 @@ + + + diff --git a/ZLPhotoBrowser/Info.plist b/ZLPhotoBrowser/Info.plist index 088c9523..fd1f7787 100644 --- a/ZLPhotoBrowser/Info.plist +++ b/ZLPhotoBrowser/Info.plist @@ -19,7 +19,7 @@ CFBundlePackageType APPL CFBundleShortVersionString - 2.5.0 + 2.5.0.1 CFBundleSignature ???? CFBundleVersion diff --git a/ZLPhotoBrowser/ViewController.m b/ZLPhotoBrowser/ViewController.m index 7f13562d..3ffd7fa3 100644 --- a/ZLPhotoBrowser/ViewController.m +++ b/ZLPhotoBrowser/ViewController.m @@ -13,6 +13,8 @@ #import "YYFPSLabel.h" #import #import "ZLPhotoModel.h" +#import "ZLPhotoManager.h" +#import "ZLProgressHUD.h" /////////////////////////////////////////////////// // git 地址: https://github.com/longitachi/ZLPhotoBrowser @@ -40,6 +42,7 @@ @interface ViewController () @property (weak, nonatomic) IBOutlet UISwitch *allowSlideSelectSwitch; @property (weak, nonatomic) IBOutlet UISwitch *allowEditVideoSwitch; @property (weak, nonatomic) IBOutlet UISwitch *allowDragSelectSwitch; +@property (weak, nonatomic) IBOutlet UISwitch *allowAnialysisAssetSwitch; @property (nonatomic, strong) NSMutableArray *lastSelectPhotos; @@ -121,6 +124,8 @@ - (ZLPhotoActionSheet *)getPas actionSheet.showSelectedMask = self.maskSwitch.isOn; //遮罩层颜色 // actionSheet.selectedMaskColor = [UIColor orangeColor]; + //允许框架解析图片 + actionSheet.shouldAnialysisAsset = self.allowAnialysisAssetSwitch.isOn; #pragma required //如果调用的方法没有传sender,则该属性必须提前赋值 actionSheet.sender = self; @@ -136,11 +141,31 @@ - (ZLPhotoActionSheet *)getPas strongSelf.lastSelectPhotos = images.mutableCopy; [strongSelf.collectionView reloadData]; NSLog(@"image:%@", images); + //解析图片 + if (!strongSelf.allowAnialysisAssetSwitch.isOn) { + [strongSelf anialysisAssets:assets original:isOriginal]; + } }]; return actionSheet; } +- (void)anialysisAssets:(NSArray *)assets original:(BOOL)original +{ + ZLProgressHUD *hud = [[ZLProgressHUD alloc] init]; + //该hud自动15s消失,请使用自己项目中的hud控件 + [hud show]; + + zl_weakify(self); + [ZLPhotoManager anialysisAssets:assets original:original completion:^(NSArray *images) { + zl_strongify(weakSelf); + [hud hide]; + strongSelf.arrDataSources = images; + [strongSelf.collectionView reloadData]; + NSLog(@"%@", images); + }]; +} + - (IBAction)btnSelectPhotoPreview:(id)sender { [self showWithPreview:YES];