Skip to content

Commit

Permalink
加入用NSOperationQueue控制获取原图并发数降低内存的示例;发布3.1.9版本
Browse files Browse the repository at this point in the history
  • Loading branch information
banchichen committed Jan 14, 2019
1 parent 0737c61 commit c5e87ef
Show file tree
Hide file tree
Showing 14 changed files with 128 additions and 30 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ A:不要去拿PHImageFileURLKey,没用的,只有通过Photos框架才能

## 六. Release Notes 最近更新

3.1.9 加入用NSOperationQueue控制获取原图并发数降低内存的示例
3.1.8 批量获取图片时加入队列控制,尝试优化大批量选择图片时CPU和内存占用过高的问题(仍然危险,maxImagesCount谨慎设置过大...)
3.1.5 相册内无照片时给出提示,修复快速滑动时内存一直增加的问题
3.1.3 适配阿拉伯等语言下从右往左布局的特性
Expand Down
4 changes: 2 additions & 2 deletions TZImagePickerController.podspec
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
Pod::Spec.new do |s|
s.name = "TZImagePickerController"
s.version = "3.1.8"
s.version = "3.1.9"
s.summary = "A clone of UIImagePickerController, support picking multiple photos、original photo and video"
s.homepage = "https://github.com/banchichen/TZImagePickerController"
s.license = "MIT"
s.author = { "banchichen" => "tanzhenios@foxmail.com" }
s.platform = :ios
s.ios.deployment_target = "8.0"
s.source = { :git => "https://github.com/banchichen/TZImagePickerController.git", :tag => "3.1.8" }
s.source = { :git => "https://github.com/banchichen/TZImagePickerController.git", :tag => "3.1.9" }
s.requires_arc = true
s.resources = "TZImagePickerController/TZImagePickerController/*.{png,bundle}"
s.source_files = "TZImagePickerController/TZImagePickerController/*.{h,m}"
Expand Down
6 changes: 6 additions & 0 deletions TZImagePickerController.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
900EF00F1C2C0B1100EA709B /* TZAssetModel.m in Sources */ = {isa = PBXBuildFile; fileRef = 900EF00E1C2C0B1100EA709B /* TZAssetModel.m */; };
900EF0121C2C107400EA709B /* TZPhotoPreviewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 900EF0111C2C107400EA709B /* TZPhotoPreviewController.m */; };
900EF0161C2C134900EA709B /* TZPhotoPreviewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 900EF0141C2C134900EA709B /* TZPhotoPreviewCell.m */; };
9019FD8D21EC25D7009ADEAE /* TZImageUploadOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = 9019FD8C21EC25D7009ADEAE /* TZImageUploadOperation.m */; };
901CC3FA21CB757500C55443 /* TZImageRequestOperation.h in Headers */ = {isa = PBXBuildFile; fileRef = 901CC3F821CB757500C55443 /* TZImageRequestOperation.h */; };
901CC3FB21CB757500C55443 /* TZImageRequestOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = 901CC3F921CB757500C55443 /* TZImageRequestOperation.m */; };
901CC3FC21CB758500C55443 /* TZImageRequestOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = 901CC3F921CB757500C55443 /* TZImageRequestOperation.m */; };
Expand Down Expand Up @@ -155,6 +156,8 @@
900EF0111C2C107400EA709B /* TZPhotoPreviewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TZPhotoPreviewController.m; sourceTree = "<group>"; };
900EF0131C2C134800EA709B /* TZPhotoPreviewCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TZPhotoPreviewCell.h; sourceTree = "<group>"; };
900EF0141C2C134900EA709B /* TZPhotoPreviewCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TZPhotoPreviewCell.m; sourceTree = "<group>"; };
9019FD8B21EC25D7009ADEAE /* TZImageUploadOperation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TZImageUploadOperation.h; sourceTree = "<group>"; };
9019FD8C21EC25D7009ADEAE /* TZImageUploadOperation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TZImageUploadOperation.m; sourceTree = "<group>"; };
901CC3F821CB757500C55443 /* TZImageRequestOperation.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TZImageRequestOperation.h; sourceTree = "<group>"; };
901CC3F921CB757500C55443 /* TZImageRequestOperation.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = TZImageRequestOperation.m; sourceTree = "<group>"; };
901F2290215CABD600F604ED /* FLAnimatedImage.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FLAnimatedImage.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -254,6 +257,8 @@
900E65831C2BB8D5003D9A9E /* AppDelegate.m */,
900E65851C2BB8D5003D9A9E /* ViewController.h */,
900E65861C2BB8D5003D9A9E /* ViewController.m */,
9019FD8B21EC25D7009ADEAE /* TZImageUploadOperation.h */,
9019FD8C21EC25D7009ADEAE /* TZImageUploadOperation.m */,
6D32B9BB1CD83640005CE1E0 /* LxGridViewFlowLayout.h */,
6D32B9BC1CD83640005CE1E0 /* LxGridViewFlowLayout.m */,
9038D58F1C3974F0007DE549 /* TZTestCell.h */,
Expand Down Expand Up @@ -583,6 +588,7 @@
901F2295215CABD600F604ED /* FLAnimatedImage.m in Sources */,
900E65841C2BB8D5003D9A9E /* AppDelegate.m in Sources */,
901F2296215CABD600F604ED /* FLAnimatedImageView.m in Sources */,
9019FD8D21EC25D7009ADEAE /* TZImageUploadOperation.m in Sources */,
900E65811C2BB8D5003D9A9E /* main.m in Sources */,
900EF0121C2C107400EA709B /* TZPhotoPreviewController.m in Sources */,
901CC3FC21CB758500C55443 /* TZImageRequestOperation.m in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion TZImagePickerController/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>3.1.8</string>
<string>3.1.9</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
/// 如下两个方法completion一般会调多次,一般会先返回缩略图,再返回原图(详见方法内部使用的系统API的说明),如果info[PHImageResultIsDegradedKey] 为 YES,则表明当前返回的是缩略图,否则是原图。
- (PHImageRequestID)getOriginalPhotoWithAsset:(PHAsset *)asset completion:(void (^)(UIImage *photo,NSDictionary *info))completion;
- (PHImageRequestID)getOriginalPhotoWithAsset:(PHAsset *)asset newCompletion:(void (^)(UIImage *photo,NSDictionary *info,BOOL isDegraded))completion;
- (PHImageRequestID)getOriginalPhotoWithAsset:(PHAsset *)asset progressHandler:(void (^)(double progress, NSError *error, BOOL *stop, NSDictionary *info))progressHandler newCompletion:(void (^)(UIImage *photo,NSDictionary *info,BOOL isDegraded))completion;
// 该方法中,completion只会走一次
- (PHImageRequestID)getOriginalPhotoDataWithAsset:(PHAsset *)asset completion:(void (^)(NSData *data,NSDictionary *info,BOOL isDegraded))completion;
- (PHImageRequestID)getOriginalPhotoDataWithAsset:(PHAsset *)asset progressHandler:(void (^)(double progress, NSError *error, BOOL *stop, NSDictionary *info))progressHandler completion:(void (^)(NSData *data,NSDictionary *info,BOOL isDegraded))completion;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,15 @@ - (PHImageRequestID)getOriginalPhotoWithAsset:(PHAsset *)asset completion:(void
}

- (PHImageRequestID)getOriginalPhotoWithAsset:(PHAsset *)asset newCompletion:(void (^)(UIImage *photo,NSDictionary *info,BOOL isDegraded))completion {
return [self getOriginalPhotoWithAsset:asset progressHandler:nil newCompletion:completion];
}

- (PHImageRequestID)getOriginalPhotoWithAsset:(PHAsset *)asset progressHandler:(void (^)(double progress, NSError *error, BOOL *stop, NSDictionary *info))progressHandler newCompletion:(void (^)(UIImage *photo,NSDictionary *info,BOOL isDegraded))completion {
PHImageRequestOptions *option = [[PHImageRequestOptions alloc]init];
option.networkAccessAllowed = YES;
if (progressHandler) {
[option setProgressHandler:progressHandler];
}
option.resizeMode = PHImageRequestOptionsResizeModeFast;
return [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeAspectFit options:option resultHandler:^(UIImage *result, NSDictionary *info) {
BOOL downloadFinined = (![[info objectForKey:PHImageCancelledKey] boolValue] && ![info objectForKey:PHImageErrorKey]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//
// Created by 谭真 on 15/12/24.
// Copyright © 2015年 谭真. All rights reserved.
// version 3.1.8 - 2019.01.14
// version 3.1.9 - 2019.01.14
// 更多信息,请前往项目的github地址:https://github.com/banchichen/TZImagePickerController

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//
// Created by 谭真 on 15/12/24.
// Copyright © 2015年 谭真. All rights reserved.
// version 3.1.8 - 2019.01.14
// version 3.1.9 - 2019.01.14
// 更多信息,请前往项目的github地址:https://github.com/banchichen/TZImagePickerController

#import "TZImagePickerController.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ NS_ASSUME_NONNULL_BEGIN
typedef void(^TZImageRequestCompletedBlock)(UIImage *photo, NSDictionary *info, BOOL isDegraded);
typedef void(^TZImageRequestProgressBlock)(double progress, NSError *error, BOOL *stop, NSDictionary *info);

@property (nonatomic, copy, nullable) TZImageRequestCompletedBlock completedBlock;
@property (nonatomic, copy, nullable) TZImageRequestProgressBlock progressBlock;
@property (nonatomic, strong, nullable) PHAsset *asset;

@property (assign, nonatomic, getter = isExecuting) BOOL executing;
@property (assign, nonatomic, getter = isFinished) BOOL finished;

- (instancetype)initWithAsset:(PHAsset *)asset completion:(TZImageRequestCompletedBlock)completionBlock progressHandler:(TZImageRequestProgressBlock)progressHandler;
- (void)done;
@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@
#import "TZImageRequestOperation.h"
#import "TZImageManager.h"

@interface TZImageRequestOperation ()
@property (nonatomic, copy) TZImageRequestCompletedBlock completedBlock;
@property (nonatomic, copy) TZImageRequestProgressBlock progressBlock;
@property (nonatomic, strong) PHAsset *asset;

@property (assign, nonatomic, getter = isExecuting) BOOL executing;
@property (assign, nonatomic, getter = isFinished) BOOL finished;
@end

@implementation TZImageRequestOperation

@synthesize executing = _executing;
Expand Down
17 changes: 17 additions & 0 deletions TZImagePickerController/TZImageUploadOperation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// TZImageUploadOperation.h
// TZImagePickerController
//
// Created by 谭真 on 2019/1/14.
// Copyright © 2019 谭真. All rights reserved.
//

#import "TZImageRequestOperation.h"

NS_ASSUME_NONNULL_BEGIN

@interface TZImageUploadOperation : TZImageRequestOperation

@end

NS_ASSUME_NONNULL_END
69 changes: 69 additions & 0 deletions TZImagePickerController/TZImageUploadOperation.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//
// TZImageUploadOperation.m
// TZImagePickerController
//
// Created by 谭真 on 2019/1/14.
// Copyright © 2019 谭真. All rights reserved.
//

#import "TZImageUploadOperation.h"
#import "TZImageManager.h"

@implementation TZImageUploadOperation

- (void)start {
NSLog(@"TZImageUploadOperation start");
self.executing = YES;
dispatch_async(dispatch_get_global_queue(0, 0), ^{
#pragma mark - 获取&上传大图
/*
[[TZImageManager manager] getPhotoWithAsset:self.asset completion:^(UIImage *photo, NSDictionary *info, BOOL isDegraded) {
dispatch_async(dispatch_get_main_queue(), ^{
if (!isDegraded) {
if (self.completedBlock) {
self.completedBlock(photo, info, isDegraded);
}
// 在这里上传图片(代码略),图片上传完毕后调用[self done]
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self done];
});
}
});
} progressHandler:^(double progress, NSError *error, BOOL *stop, NSDictionary *info) {
dispatch_async(dispatch_get_main_queue(), ^{
if (self.progressBlock) {
self.progressBlock(progress, error, stop, info);
}
});
} networkAccessAllowed:YES];
*/

#pragma mark - 获取&上传原图
[[TZImageManager manager] getOriginalPhotoWithAsset:self.asset progressHandler:^(double progress, NSError *error, BOOL *stop, NSDictionary *info) {
dispatch_async(dispatch_get_main_queue(), ^{
if (self.progressBlock) {
self.progressBlock(progress, error, stop, info);
}
});
} newCompletion:^(UIImage *photo, NSDictionary *info, BOOL isDegraded) {
dispatch_async(dispatch_get_main_queue(), ^{
if (!isDegraded) {
if (self.completedBlock) {
self.completedBlock(photo, info, isDegraded);
}
// 在这里上传图片(代码略),图片上传完毕后调用[self done]
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self done];
});
}
});
}];
});
}

- (void)done {
[super done];
// NSLog(@"TZImageUploadOperation done");
}

@end
28 changes: 13 additions & 15 deletions TZImagePickerController/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#import "TZAssetCell.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import "FLAnimatedImage.h"
#import "TZImageUploadOperation.h"

@interface ViewController ()<TZImagePickerControllerDelegate,UICollectionViewDataSource,UICollectionViewDelegate,UIImagePickerControllerDelegate,UIAlertViewDelegate,UINavigationControllerDelegate> {
NSMutableArray *_selectedPhotos;
Expand All @@ -34,6 +35,8 @@ @interface ViewController ()<TZImagePickerControllerDelegate,UICollectionViewDat
@property (strong, nonatomic) LxGridViewFlowLayout *layout;
@property (strong, nonatomic) CLLocation *location;

@property (nonatomic, strong) NSOperationQueue *operationQueue;

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
// 设置开关
@property (weak, nonatomic) IBOutlet UISwitch *showTakePhotoBtnSwitch; ///< 允许拍照
Expand Down Expand Up @@ -556,25 +559,20 @@ - (void)imagePickerController:(TZImagePickerController *)picker didFinishPicking
NSLog(@"location:%@",phAsset.location);
}

/*
// 3. 获取原图的示例,这样一次性获取很可能会导致内存飙升,建议获取1-2张,消费和释放掉,再获取剩下的
__block NSMutableArray *originalPhotos = [NSMutableArray array];
__block NSInteger finishCount = 0;
for (NSInteger i = 0; i < assets.count; i++) {
[originalPhotos addObject:@1];
}
// 3. 获取原图的示例,用队列限制最大并发为1,避免内存暴增
self.operationQueue = [[NSOperationQueue alloc] init];
self.operationQueue.maxConcurrentOperationCount = 1;
for (NSInteger i = 0; i < assets.count; i++) {
PHAsset *asset = assets[i];
PHImageRequestID requestId = [[TZImageManager manager] getOriginalPhotoWithAsset:asset completion:^(UIImage *photo, NSDictionary *info) {
finishCount += 1;
[originalPhotos replaceObjectAtIndex:i withObject:photo];
if (finishCount >= assets.count) {
NSLog(@"All finished.");
}
// 图片上传operation,上传代码请写到operation内的start方法里,内有注释
TZImageUploadOperation *operation = [[TZImageUploadOperation alloc] initWithAsset:asset completion:^(UIImage * photo, NSDictionary *info, BOOL isDegraded) {
if (isDegraded) return;
NSLog(@"图片获取&上传完成");
} progressHandler:^(double progress, NSError * _Nonnull error, BOOL * _Nonnull stop, NSDictionary * _Nonnull info) {
NSLog(@"获取原图进度 %f", progress);
}];
NSLog(@"requestId: %d", requestId);
[self.operationQueue addOperation:operation];
}
*/
}

// If user picking a video and allowPickingMultipleVideo is NO, this callback will be called.
Expand Down
2 changes: 1 addition & 1 deletion TZImagePickerControllerFramework/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>3.1.8</string>
<string>3.1.9</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
Expand Down

0 comments on commit c5e87ef

Please sign in to comment.