Commit 1b52982a authored by banchichen's avatar banchichen

优化权限判断相关代码

parent ef2b14a2
...@@ -45,7 +45,7 @@ ...@@ -45,7 +45,7 @@
- (void)getOriginalPhotoWithAsset:(id)asset completion:(void (^)(UIImage *photo,NSDictionary *info))completion; - (void)getOriginalPhotoWithAsset:(id)asset completion:(void (^)(UIImage *photo,NSDictionary *info))completion;
/// Save photo 保存照片 /// Save photo 保存照片
- (void)savePhotoWithImage:(UIImage *)image completion:(void (^)())completion; - (void)savePhotoWithImage:(UIImage *)image completion:(void (^)(NSError *error))completion;
/// Get video 获得视频 /// Get video 获得视频
- (void)getVideoWithAsset:(id)asset completion:(void (^)(AVPlayerItem * playerItem, NSDictionary * info))completion; - (void)getVideoWithAsset:(id)asset completion:(void (^)(AVPlayerItem * playerItem, NSDictionary * info))completion;
......
...@@ -509,7 +509,7 @@ static CGFloat TZScreenScale; ...@@ -509,7 +509,7 @@ static CGFloat TZScreenScale;
#pragma mark - Save photo #pragma mark - Save photo
- (void)savePhotoWithImage:(UIImage *)image completion:(void (^)())completion { - (void)savePhotoWithImage:(UIImage *)image completion:(void (^)(NSError *error))completion {
NSData *data = UIImageJPEGRepresentation(image, 0.9); NSData *data = UIImageJPEGRepresentation(image, 0.9);
if (iOS9Later) { // 这里有坑... iOS8系统下这个方法保存图片会失败 if (iOS9Later) { // 这里有坑... iOS8系统下这个方法保存图片会失败
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
...@@ -519,9 +519,12 @@ static CGFloat TZScreenScale; ...@@ -519,9 +519,12 @@ static CGFloat TZScreenScale;
} completionHandler:^(BOOL success, NSError * _Nullable error) { } completionHandler:^(BOOL success, NSError * _Nullable error) {
dispatch_sync(dispatch_get_main_queue(), ^{ dispatch_sync(dispatch_get_main_queue(), ^{
if (success && completion) { if (success && completion) {
completion(); completion(nil);
} else if (error) { } else if (error) {
NSLog(@"保存照片出错:%@",error.localizedDescription); NSLog(@"保存照片出错:%@",error.localizedDescription);
if (completion) {
completion(error);
}
} }
}); });
}]; }];
...@@ -532,9 +535,12 @@ static CGFloat TZScreenScale; ...@@ -532,9 +535,12 @@ static CGFloat TZScreenScale;
#pragma clang diagnostic pop #pragma clang diagnostic pop
if (error) { if (error) {
NSLog(@"保存图片失败:%@",error.localizedDescription); NSLog(@"保存图片失败:%@",error.localizedDescription);
if (completion) {
completion(error);
}
} else { } else {
if (completion) { if (completion) {
completion(); completion(nil);
} }
} }
}]; }];
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
@interface TZImagePickerController () { @interface TZImagePickerController () {
NSTimer *_timer; NSTimer *_timer;
UILabel *_tipLable; UILabel *_tipLable;
UIButton *_settingBtn;
BOOL _pushToPhotoPickerVc; BOOL _pushToPhotoPickerVc;
UIButton *_progressHUD; UIButton *_progressHUD;
...@@ -103,7 +104,7 @@ ...@@ -103,7 +104,7 @@
if (![[TZImageManager manager] authorizationStatusAuthorized]) { if (![[TZImageManager manager] authorizationStatusAuthorized]) {
_tipLable = [[UILabel alloc] init]; _tipLable = [[UILabel alloc] init];
_tipLable.frame = CGRectMake(8, 0, self.view.tz_width - 16, 300); _tipLable.frame = CGRectMake(8, 120, self.view.tz_width - 16, 60);
_tipLable.textAlignment = NSTextAlignmentCenter; _tipLable.textAlignment = NSTextAlignmentCenter;
_tipLable.numberOfLines = 0; _tipLable.numberOfLines = 0;
_tipLable.font = [UIFont systemFontOfSize:16]; _tipLable.font = [UIFont systemFontOfSize:16];
...@@ -114,6 +115,13 @@ ...@@ -114,6 +115,13 @@
_tipLable.text = tipText; _tipLable.text = tipText;
[self.view addSubview:_tipLable]; [self.view addSubview:_tipLable];
_settingBtn = [UIButton buttonWithType:UIButtonTypeSystem];
[_settingBtn setTitle:[NSBundle tz_localizedStringForKey:@"Setting"] forState:UIControlStateNormal];
_settingBtn.frame = CGRectMake(0, 180, self.view.tz_width, 44);
_settingBtn.titleLabel.font = [UIFont systemFontOfSize:18];
[_settingBtn addTarget:self action:@selector(settingBtnClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_settingBtn];
_timer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(observeAuthrizationStatusChange) userInfo:nil repeats:YES]; _timer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(observeAuthrizationStatusChange) userInfo:nil repeats:YES];
} else { } else {
[self pushToPhotoPickerVc]; [self pushToPhotoPickerVc];
...@@ -164,6 +172,7 @@ ...@@ -164,6 +172,7 @@
if ([[TZImageManager manager] authorizationStatusAuthorized]) { if ([[TZImageManager manager] authorizationStatusAuthorized]) {
[self pushToPhotoPickerVc]; [self pushToPhotoPickerVc];
[_tipLable removeFromSuperview]; [_tipLable removeFromSuperview];
[_settingBtn removeFromSuperview];
[_timer invalidate]; [_timer invalidate];
_timer = nil; _timer = nil;
} }
...@@ -285,6 +294,14 @@ ...@@ -285,6 +294,14 @@
[TZImageManager manager].sortAscendingByModificationDate = sortAscendingByModificationDate; [TZImageManager manager].sortAscendingByModificationDate = sortAscendingByModificationDate;
} }
- (void)settingBtnClick {
if (iOS8Later) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
} else {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=Privacy&path=PHOTOS"]];
}
}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated { - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
if (iOS7Later) viewController.automaticallyAdjustsScrollViewInsets = NO; if (iOS7Later) viewController.automaticallyAdjustsScrollViewInsets = NO;
if (_timer) { [_timer invalidate]; _timer = nil;} if (_timer) { [_timer invalidate]; _timer = nil;}
......
...@@ -418,7 +418,7 @@ static CGSize AssetGridThumbnailSize; ...@@ -418,7 +418,7 @@ static CGSize AssetGridThumbnailSize;
- (void)takePhoto { - (void)takePhoto {
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if ((authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied) && iOS8Later) { if ((authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied) && iOS7Later) {
#pragma clang diagnostic push #pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations" #pragma clang diagnostic ignored "-Wdeprecated-declarations"
// 无权限 做一个友好的提示 // 无权限 做一个友好的提示
...@@ -531,7 +531,7 @@ static CGSize AssetGridThumbnailSize; ...@@ -531,7 +531,7 @@ static CGSize AssetGridThumbnailSize;
if (iOS8Later) { if (iOS8Later) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
} else { } else {
// [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=Privacy&path=Photos"]]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=Privacy&path=CAMERA"]];
} }
} }
} }
...@@ -545,8 +545,10 @@ static CGSize AssetGridThumbnailSize; ...@@ -545,8 +545,10 @@ static CGSize AssetGridThumbnailSize;
TZImagePickerController *imagePickerVc = (TZImagePickerController *)self.navigationController; TZImagePickerController *imagePickerVc = (TZImagePickerController *)self.navigationController;
[imagePickerVc showProgressHUD]; [imagePickerVc showProgressHUD];
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[[TZImageManager manager] savePhotoWithImage:image completion:^{ [[TZImageManager manager] savePhotoWithImage:image completion:^(NSError *error){
[self reloadPhotoArray]; if (!error) {
[self reloadPhotoArray];
}
}]; }];
} }
} }
......
...@@ -215,7 +215,7 @@ ...@@ -215,7 +215,7 @@
- (void)takePhoto { - (void)takePhoto {
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if ((authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied) && iOS8Later) { if ((authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied) && iOS7Later) {
// 无权限 做一个友好的提示 // 无权限 做一个友好的提示
#pragma clang diagnostic push #pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations" #pragma clang diagnostic ignored "-Wdeprecated-declarations"
...@@ -245,19 +245,27 @@ ...@@ -245,19 +245,27 @@
[tzImagePickerVc showProgressHUD]; [tzImagePickerVc showProgressHUD];
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
// save photo and get asset / 保存图片,获取到asset // save photo and get asset / 保存图片,获取到asset
[[TZImageManager manager] savePhotoWithImage:image completion:^{ [[TZImageManager manager] savePhotoWithImage:image completion:^(NSError *error){
[[TZImageManager manager] getCameraRollAlbum:NO allowPickingImage:YES completion:^(TZAlbumModel *model) { if (error) { // 如果保存失败,基本是没有相册权限导致的...
[[TZImageManager manager] getAssetsFromFetchResult:model.result allowPickingVideo:NO allowPickingImage:YES completion:^(NSArray<TZAssetModel *> *models) { [tzImagePickerVc hideProgressHUD];
[tzImagePickerVc hideProgressHUD]; NSLog(@"图片保存失败 %@",error);
TZAssetModel *assetModel = [models firstObject]; UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"无法保存图片" message:@"请在iPhone的""设置-隐私-相册""中允许访问相册" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"设置", nil];
if (tzImagePickerVc.sortAscendingByModificationDate) { alert.tag = 1;
assetModel = [models lastObject]; [alert show];
} } else {
[_selectedAssets addObject:assetModel.asset]; [[TZImageManager manager] getCameraRollAlbum:NO allowPickingImage:YES completion:^(TZAlbumModel *model) {
[_selectedPhotos addObject:image]; [[TZImageManager manager] getAssetsFromFetchResult:model.result allowPickingVideo:NO allowPickingImage:YES completion:^(NSArray<TZAssetModel *> *models) {
[_collectionView reloadData]; [tzImagePickerVc hideProgressHUD];
TZAssetModel *assetModel = [models firstObject];
if (tzImagePickerVc.sortAscendingByModificationDate) {
assetModel = [models lastObject];
}
[_selectedAssets addObject:assetModel.asset];
[_selectedPhotos addObject:image];
[_collectionView reloadData];
}];
}]; }];
}]; }
}]; }];
} }
} }
...@@ -291,7 +299,11 @@ ...@@ -291,7 +299,11 @@
if (iOS8Later) { if (iOS8Later) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
} else { } else {
// [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=Privacy&path=Photos"]]; if (alertView.tag == 1) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=Privacy&path=PHOTOS"]];
} else {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=Privacy&path=CAMERA"]];
}
} }
} }
} }
......
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