Commit 5a57f3df authored by yuzheng's avatar yuzheng

尝试在批量获取图片时加一个队列控制

parent fa4ce1c0
......@@ -29,6 +29,9 @@
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 */; };
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 */; };
901F2295215CABD600F604ED /* FLAnimatedImage.m in Sources */ = {isa = PBXBuildFile; fileRef = 901F2291215CABD600F604ED /* FLAnimatedImage.m */; };
901F2296215CABD600F604ED /* FLAnimatedImageView.m in Sources */ = {isa = PBXBuildFile; fileRef = 901F2293215CABD600F604ED /* FLAnimatedImageView.m */; };
902938D81EE2D02C000F2F8F /* TZLocationManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 902938D71EE2D02C000F2F8F /* TZLocationManager.m */; };
......@@ -152,6 +155,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>"; };
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>"; };
901F2291215CABD600F604ED /* FLAnimatedImage.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FLAnimatedImage.m; sourceTree = "<group>"; };
901F2292215CABD600F604ED /* FLAnimatedImageView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FLAnimatedImageView.h; sourceTree = "<group>"; };
......@@ -314,6 +319,8 @@
6DD0502A1DF659ED0057C78D /* TZProgressView.m */,
90CE84AC1C3A89EF003D0779 /* TZImageManager.h */,
90CE84AD1C3A89EF003D0779 /* TZImageManager.m */,
901CC3F821CB757500C55443 /* TZImageRequestOperation.h */,
901CC3F921CB757500C55443 /* TZImageRequestOperation.m */,
6DC84D081DF5358500A107A9 /* TZImageCropManager.h */,
6DC84D091DF5358500A107A9 /* TZImageCropManager.m */,
902938D61EE2D02C000F2F8F /* TZLocationManager.h */,
......@@ -370,6 +377,7 @@
isa = PBXHeadersBuildPhase;
buildActionMask = 2147483647;
files = (
901CC3FA21CB757500C55443 /* TZImageRequestOperation.h in Headers */,
9F763A6B1FA0731E00D9E526 /* TZImagePickerController.h in Headers */,
9F763A6D1FA0733700D9E526 /* NSBundle+TZImagePicker.h in Headers */,
9F763A6C1FA0733100D9E526 /* TZAssetModel.h in Headers */,
......@@ -577,6 +585,7 @@
901F2296215CABD600F604ED /* FLAnimatedImageView.m in Sources */,
900E65811C2BB8D5003D9A9E /* main.m in Sources */,
900EF0121C2C107400EA709B /* TZPhotoPreviewController.m in Sources */,
901CC3FC21CB758500C55443 /* TZImageRequestOperation.m in Sources */,
6D5358CD1D64600F00928CC6 /* NSBundle+TZImagePicker.m in Sources */,
900EEFF81C2BBF9500EA709B /* TZImagePickerController.m in Sources */,
902938D81EE2D02C000F2F8F /* TZLocationManager.m in Sources */,
......@@ -604,6 +613,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
901CC3FB21CB757500C55443 /* TZImageRequestOperation.m in Sources */,
9F763A501FA072E500D9E526 /* TZImagePickerController.m in Sources */,
9F763A521FA072E500D9E526 /* TZPhotoPickerController.m in Sources */,
9F763A541FA072E500D9E526 /* TZAssetCell.m in Sources */,
......
//
// TZImageRequestOperation.h
// TZImagePickerControllerFramework
//
// Created by 谭真 on 2018/12/20.
// Copyright © 2018 谭真. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <Photos/Photos.h>
NS_ASSUME_NONNULL_BEGIN
@interface TZImageRequestOperation : NSOperation
typedef void(^TZImageRequestCompletedBlock)(UIImage *photo, NSDictionary *info, BOOL isDegraded);
typedef void(^TZImageRequestProgressBlock)(double progress, NSError *error, BOOL *stop, NSDictionary *info);
- (instancetype)initWithAsset:(PHAsset *)asset completion:(TZImageRequestCompletedBlock)completionBlock progressHandler:(TZImageRequestProgressBlock)progressHandler;
@end
NS_ASSUME_NONNULL_END
//
// TZImageRequestOperation.m
// TZImagePickerControllerFramework
//
// Created by 谭真 on 2018/12/20.
// Copyright © 2018 谭真. All rights reserved.
//
#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;
@synthesize finished = _finished;
- (instancetype)initWithAsset:(PHAsset *)asset completion:(TZImageRequestCompletedBlock)completionBlock progressHandler:(TZImageRequestProgressBlock)progressHandler {
self = [super init];
self.asset = asset;
self.completedBlock = completionBlock;
self.progressBlock = progressHandler;
_executing = NO;
_finished = NO;
return self;
}
- (void)start {
self.executing = YES;
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[[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);
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * 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];
});
}
- (void)done {
self.finished = YES;
self.executing = NO;
[self reset];
}
- (void)reset {
self.asset = nil;
self.completedBlock = nil;
self.progressBlock = nil;
}
- (void)setFinished:(BOOL)finished {
[self willChangeValueForKey:@"isFinished"];
_finished = finished;
[self didChangeValueForKey:@"isFinished"];
}
- (void)setExecuting:(BOOL)executing {
[self willChangeValueForKey:@"isExecuting"];
_executing = executing;
[self didChangeValueForKey:@"isExecuting"];
}
- (BOOL)isAsynchronous {
return YES;
}
@end
......@@ -17,6 +17,7 @@
#import "TZGifPhotoPreviewController.h"
#import "TZLocationManager.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import "TZImageRequestOperation.h"
@interface TZPhotoPickerController ()<UICollectionViewDataSource,UICollectionViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIAlertViewDelegate> {
NSMutableArray *_models;
......@@ -42,6 +43,7 @@
@property (strong, nonatomic) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) UIImagePickerController *imagePickerVc;
@property (strong, nonatomic) CLLocation *location;
@property (nonatomic, strong) NSOperationQueue *operationQueue;
@end
static CGSize AssetGridThumbnailSize;
......@@ -93,6 +95,9 @@ static CGFloat itemMargin = 5;
_showTakePhotoBtn = _model.isCameraRoll && ((tzImagePickerVc.allowTakePicture && tzImagePickerVc.allowPickingImage) || (tzImagePickerVc.allowTakeVideo && tzImagePickerVc.allowPickingVideo));
// [self resetCachedAssets];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didChangeStatusBarOrientationNotification:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
self.operationQueue = [[NSOperationQueue alloc] init];
self.operationQueue.maxConcurrentOperationCount = 3;
}
- (void)fetchAssetModels {
......@@ -399,7 +404,7 @@ static CGFloat itemMargin = 5;
__block UIAlertController *alertView;
for (NSInteger i = 0; i < tzImagePickerVc.selectedModels.count; i++) {
TZAssetModel *model = tzImagePickerVc.selectedModels[i];
[[TZImageManager manager] getPhotoWithAsset:model.asset completion:^(UIImage *photo, NSDictionary *info, BOOL isDegraded) {
TZImageRequestOperation *operation = [[TZImageRequestOperation alloc] initWithAsset:model.asset completion:^(UIImage * _Nonnull photo, NSDictionary * _Nonnull info, BOOL isDegraded) {
if (isDegraded) return;
if (photo) {
if (![TZImagePickerConfig sharedInstance].notScaleImage) {
......@@ -416,7 +421,7 @@ static CGFloat itemMargin = 5;
[tzImagePickerVc hideAlertView:alertView];
[self didGetAllPhotos:photos assets:assets infoArr:infoArr];
}
} progressHandler:^(double progress, NSError *error, BOOL *stop, NSDictionary *info) {
} progressHandler:^(double progress, NSError * _Nonnull error, BOOL * _Nonnull stop, NSDictionary * _Nonnull info) {
// 如果图片正在从iCloud同步中,提醒用户
if (progress < 1 && havenotShowAlert && !alertView) {
[tzImagePickerVc hideProgressHUD];
......@@ -427,7 +432,8 @@ static CGFloat itemMargin = 5;
if (progress >= 1) {
havenotShowAlert = YES;
}
} networkAccessAllowed:YES];
}];
[self.operationQueue addOperation:operation];
}
}
if (tzImagePickerVc.selectedModels.count <= 0 || tzImagePickerVc.onlyReturnAsset) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment