Skip to content

Commit 4cb1d87

Browse files
committed
优化图片保存策略,最多等待10次尝试获取PHAsset,发布3.4.2版本
1 parent 09a3ca0 commit 4cb1d87

8 files changed

Lines changed: 37 additions & 16 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
## 重要提示1:提issue前,请先对照Demo、常见问题自查!Demo正常说明你可以升级下新版试试。
1010

11-
## 重要提示2:3.4.1版本适配了iOS14,修复2个严重问题,强烈建议尽快更新
11+
## 重要提示2:3.4.2版本适配了iOS14,修复2个严重问题,强烈建议尽快更新
1212

1313
关于升级iOS10和Xcdoe8的提示:
1414
在Xcode8环境下将项目运行在iOS10的设备/模拟器中,访问相册和相机需要额外配置info.plist文件。分别是Privacy - Photo Library Usage Description和Privacy - Camera Usage Description字段,详见Demo中info.plist中的设置。
@@ -123,7 +123,7 @@ A:不要去拿PHImageFileURLKey,没用的,只有通过Photos框架才能
123123

124124
## 六. Release Notes 最近更新
125125

126-
**3.4.1 适配iOS14,若干问题修复**
126+
**3.4.2 适配iOS14,若干问题修复**
127127
3.3.2 适配iOS13,若干问题修复
128128
3.2.1 新增裁剪用scaleAspectFillCrop属性,设置为YES后,照片尺寸小于裁剪框时会自动放大撑满
129129
3.2.0 加入用NSOperationQueue控制获取原图并发数降低内存的示例

TZImagePickerController.podspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
Pod::Spec.new do |s|
22
s.name = "TZImagePickerController"
3-
s.version = "3.4.1"
3+
s.version = "3.4.2"
44
s.summary = "A clone of UIImagePickerController, support picking multiple photos、original photo and video"
55
s.homepage = "https://github.com/banchichen/TZImagePickerController"
66
s.license = "MIT"
77
s.author = { "banchichen" => "tanzhenios@foxmail.com" }
88
s.platform = :ios
99
s.ios.deployment_target = "8.0"
10-
s.source = { :git => "https://github.com/banchichen/TZImagePickerController.git", :tag => "3.4.1" }
10+
s.source = { :git => "https://github.com/banchichen/TZImagePickerController.git", :tag => "3.4.2" }
1111
s.requires_arc = true
1212
s.resources = "TZImagePickerController/TZImagePickerController/*.{png,bundle}"
1313
s.source_files = "TZImagePickerController/TZImagePickerController/*.{h,m}"

TZImagePickerController.xcodeproj/project.pbxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@
807807
INFOPLIST_FILE = TZImagePickerController/Info.plist;
808808
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
809809
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
810-
MARKETING_VERSION = 3.4.1;
810+
MARKETING_VERSION = 3.4.2;
811811
PRODUCT_BUNDLE_IDENTIFIER = tanzhenios2019.TZImagePickerController.www;
812812
PRODUCT_NAME = "$(TARGET_NAME)";
813813
PROVISIONING_PROFILE = "";
@@ -831,7 +831,7 @@
831831
INFOPLIST_FILE = TZImagePickerController/Info.plist;
832832
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
833833
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
834-
MARKETING_VERSION = 3.4.1;
834+
MARKETING_VERSION = 3.4.2;
835835
PRODUCT_BUNDLE_IDENTIFIER = tanzhenios2019.TZImagePickerController.www;
836836
PRODUCT_NAME = "$(TARGET_NAME)";
837837
PROVISIONING_PROFILE = "";
@@ -918,7 +918,7 @@
918918
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
919919
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
920920
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
921-
MARKETING_VERSION = 3.4.1;
921+
MARKETING_VERSION = 3.4.2;
922922
OTHER_LDFLAGS = "-all_load";
923923
PRODUCT_BUNDLE_IDENTIFIER = tanzhen.TZImagePickerControllerFramework;
924924
PRODUCT_NAME = TZImagePickerController;
@@ -959,7 +959,7 @@
959959
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
960960
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
961961
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
962-
MARKETING_VERSION = 3.4.1;
962+
MARKETING_VERSION = 3.4.2;
963963
OTHER_LDFLAGS = "-all_load";
964964
PRODUCT_BUNDLE_IDENTIFIER = tanzhen.TZImagePickerControllerFramework;
965965
PRODUCT_NAME = TZImagePickerController;

TZImagePickerController/TZImagePickerController/TZAssetModel.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ - (void)setResult:(PHFetchResult *)result needFetchAssets:(BOOL)needFetchAssets
4545

4646
- (void)refreshFetchResult {
4747
PHFetchResult *fetchResult = [PHAsset fetchAssetsInAssetCollection:self.collection options:self.options];
48+
self.count = fetchResult.count;
4849
[self setResult:fetchResult];
4950
}
5051

TZImagePickerController/TZImagePickerController/TZImageManager.m

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -485,8 +485,7 @@ - (void)savePhotoWithImage:(UIImage *)image location:(CLLocation *)location comp
485485
} completionHandler:^(BOOL success, NSError *error) {
486486
dispatch_async(dispatch_get_main_queue(), ^{
487487
if (success && completion) {
488-
PHAsset *asset = [[PHAsset fetchAssetsWithLocalIdentifiers:@[localIdentifier] options:nil] firstObject];
489-
completion(asset, nil);
488+
[self fetchAssetByIocalIdentifier:localIdentifier retryCount:10 completion:completion];
490489
} else if (error) {
491490
NSLog(@"保存照片出错:%@",error.localizedDescription);
492491
if (completion) {
@@ -522,8 +521,7 @@ - (void)savePhotoWithImage:(UIImage *)image meta:(NSDictionary *)meta location:(
522521
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];
523522
dispatch_async(dispatch_get_main_queue(), ^{
524523
if (success && completion) {
525-
PHAsset *asset = [[PHAsset fetchAssetsWithLocalIdentifiers:@[localIdentifier] options:nil] firstObject];
526-
completion(asset, nil);
524+
[self fetchAssetByIocalIdentifier:localIdentifier retryCount:10 completion:completion];
527525
} else if (error) {
528526
NSLog(@"保存照片出错:%@",error.localizedDescription);
529527
if (completion) {
@@ -534,6 +532,19 @@ - (void)savePhotoWithImage:(UIImage *)image meta:(NSDictionary *)meta location:(
534532
}];
535533
}
536534

535+
- (void)fetchAssetByIocalIdentifier:(NSString *)localIdentifier retryCount:(NSInteger)retryCount completion:(void (^)(PHAsset *asset, NSError *error))completion {
536+
PHAsset *asset = [[PHAsset fetchAssetsWithLocalIdentifiers:@[localIdentifier] options:nil] firstObject];
537+
if (asset || retryCount <= 0) {
538+
if (completion) {
539+
completion(asset, nil);
540+
}
541+
return;
542+
}
543+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
544+
[self fetchAssetByIocalIdentifier:localIdentifier retryCount:retryCount - 1 completion:completion];
545+
});
546+
}
547+
537548
#pragma mark - Save video
538549

539550
- (void)saveVideoWithUrl:(NSURL *)url completion:(void (^)(PHAsset *asset, NSError *error))completion {
@@ -552,8 +563,7 @@ - (void)saveVideoWithUrl:(NSURL *)url location:(CLLocation *)location completion
552563
} completionHandler:^(BOOL success, NSError *error) {
553564
dispatch_async(dispatch_get_main_queue(), ^{
554565
if (success && completion) {
555-
PHAsset *asset = [[PHAsset fetchAssetsWithLocalIdentifiers:@[localIdentifier] options:nil] firstObject];
556-
completion(asset, nil);
566+
[self fetchAssetByIocalIdentifier:localIdentifier retryCount:10 completion:completion];
557567
} else if (error) {
558568
NSLog(@"保存视频出错:%@",error.localizedDescription);
559569
if (completion) {

TZImagePickerController/TZImagePickerController/TZImagePickerController.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//
55
// Created by 谭真 on 15/12/24.
66
// Copyright © 2015年 谭真. All rights reserved.
7-
// version 3.4.1 - 2020.07.20
7+
// version 3.4.2 - 2020.08.17
88
// 更多信息,请前往项目的github地址:https://github.com/banchichen/TZImagePickerController
99

1010
/*

TZImagePickerController/TZImagePickerController/TZImagePickerController.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//
55
// Created by 谭真 on 15/12/24.
66
// Copyright © 2015年 谭真. All rights reserved.
7-
// version 3.4.1 - 2020.07.20
7+
// version 3.4.2 - 2020.08.17
88
// 更多信息,请前往项目的github地址:https://github.com/banchichen/TZImagePickerController
99

1010
#import "TZImagePickerController.h"

TZImagePickerController/TZImagePickerController/TZPhotoPickerController.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ @interface TZPhotoPickerController ()<UICollectionViewDataSource,UICollectionVie
4444
@property (nonatomic, strong) UIImagePickerController *imagePickerVc;
4545
@property (strong, nonatomic) CLLocation *location;
4646
@property (nonatomic, strong) NSOperationQueue *operationQueue;
47+
@property (nonatomic, assign) BOOL isSavingMedia;
4748
@end
4849

4950
static CGSize AssetGridThumbnailSize;
@@ -173,6 +174,8 @@ - (void)configCollectionView {
173174
[self.view addSubview:_collectionView];
174175
[_collectionView registerClass:[TZAssetCell class] forCellWithReuseIdentifier:@"TZAssetCell"];
175176
[_collectionView registerClass:[TZAssetCameraCell class] forCellWithReuseIdentifier:@"TZAssetCameraCell"];
177+
} else {
178+
[_collectionView reloadData];
176179
}
177180

178181
if (_showTakePhotoBtn) {
@@ -830,7 +833,9 @@ - (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingM
830833
UIImage *photo = [info objectForKey:UIImagePickerControllerOriginalImage];
831834
NSDictionary *meta = [info objectForKey:UIImagePickerControllerMediaMetadata];
832835
if (photo) {
836+
self.isSavingMedia = YES;
833837
[[TZImageManager manager] savePhotoWithImage:photo meta:meta location:self.location completion:^(PHAsset *asset, NSError *error){
838+
self.isSavingMedia = NO;
834839
if (!error && asset) {
835840
[self addPHAsset:asset];
836841
} else {
@@ -845,7 +850,9 @@ - (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingM
845850
[imagePickerVc showProgressHUD];
846851
NSURL *videoUrl = [info objectForKey:UIImagePickerControllerMediaURL];
847852
if (videoUrl) {
853+
self.isSavingMedia = YES;
848854
[[TZImageManager manager] saveVideoWithUrl:videoUrl location:self.location completion:^(PHAsset *asset, NSError *error) {
855+
self.isSavingMedia = NO;
849856
if (!error && asset) {
850857
[self addPHAsset:asset];
851858
} else {
@@ -914,6 +921,9 @@ - (void)dealloc {
914921
#pragma mark - PHPhotoLibraryChangeObserver
915922

916923
- (void)photoLibraryDidChange:(PHChange *)changeInstance {
924+
if (self.isSavingMedia) {
925+
return;
926+
}
917927
dispatch_async(dispatch_get_main_queue(), ^{
918928
[self.model refreshFetchResult];
919929
[self fetchAssetModels];

0 commit comments

Comments
 (0)