Commit 1b52982a authored by banchichen's avatar banchichen

优化权限判断相关代码

parent ef2b14a2
......@@ -45,7 +45,7 @@
- (void)getOriginalPhotoWithAsset:(id)asset completion:(void (^)(UIImage *photo,NSDictionary *info))completion;
/// Save photo 保存照片
- (void)savePhotoWithImage:(UIImage *)image completion:(void (^)())completion;
- (void)savePhotoWithImage:(UIImage *)image completion:(void (^)(NSError *error))completion;
/// Get video 获得视频
- (void)getVideoWithAsset:(id)asset completion:(void (^)(AVPlayerItem * playerItem, NSDictionary * info))completion;
......
......@@ -509,7 +509,7 @@ static CGFloat TZScreenScale;
#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);
if (iOS9Later) { // 这里有坑... iOS8系统下这个方法保存图片会失败
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
......@@ -519,9 +519,12 @@ static CGFloat TZScreenScale;
} completionHandler:^(BOOL success, NSError * _Nullable error) {
dispatch_sync(dispatch_get_main_queue(), ^{
if (success && completion) {
completion();
completion(nil);
} else if (error) {
NSLog(@"保存照片出错:%@",error.localizedDescription);
if (completion) {
completion(error);
}
}
});
}];
......@@ -532,9 +535,12 @@ static CGFloat TZScreenScale;
#pragma clang diagnostic pop
if (error) {
NSLog(@"保存图片失败:%@",error.localizedDescription);
if (completion) {
completion(error);
}
} else {
if (completion) {
completion();
completion(nil);
}
}
}];
......
......@@ -17,6 +17,7 @@
@interface TZImagePickerController () {
NSTimer *_timer;
UILabel *_tipLable;
UIButton *_settingBtn;
BOOL _pushToPhotoPickerVc;
UIButton *_progressHUD;
......@@ -103,7 +104,7 @@
if (![[TZImageManager manager] authorizationStatusAuthorized]) {
_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.numberOfLines = 0;
_tipLable.font = [UIFont systemFontOfSize:16];
......@@ -114,6 +115,13 @@
_tipLable.text = tipText;
[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];
} else {
[self pushToPhotoPickerVc];
......@@ -164,6 +172,7 @@
if ([[TZImageManager manager] authorizationStatusAuthorized]) {
[self pushToPhotoPickerVc];
[_tipLable removeFromSuperview];
[_settingBtn removeFromSuperview];
[_timer invalidate];
_timer = nil;
}
......@@ -285,6 +294,14 @@
[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 {
if (iOS7Later) viewController.automaticallyAdjustsScrollViewInsets = NO;
if (_timer) { [_timer invalidate]; _timer = nil;}
......
......@@ -418,7 +418,7 @@ static CGSize AssetGridThumbnailSize;
- (void)takePhoto {
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if ((authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied) && iOS8Later) {
if ((authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied) && iOS7Later) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
// 无权限 做一个友好的提示
......@@ -531,7 +531,7 @@ static CGSize AssetGridThumbnailSize;
if (iOS8Later) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
} 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;
TZImagePickerController *imagePickerVc = (TZImagePickerController *)self.navigationController;
[imagePickerVc showProgressHUD];
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[[TZImageManager manager] savePhotoWithImage:image completion:^{
[[TZImageManager manager] savePhotoWithImage:image completion:^(NSError *error){
if (!error) {
[self reloadPhotoArray];
}
}];
}
}
......
......@@ -215,7 +215,7 @@
- (void)takePhoto {
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if ((authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied) && iOS8Later) {
if ((authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied) && iOS7Later) {
// 无权限 做一个友好的提示
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
......@@ -245,7 +245,14 @@
[tzImagePickerVc showProgressHUD];
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
// save photo and get asset / 保存图片,获取到asset
[[TZImageManager manager] savePhotoWithImage:image completion:^{
[[TZImageManager manager] savePhotoWithImage:image completion:^(NSError *error){
if (error) { // 如果保存失败,基本是没有相册权限导致的...
[tzImagePickerVc hideProgressHUD];
NSLog(@"图片保存失败 %@",error);
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"无法保存图片" message:@"请在iPhone的""设置-隐私-相册""中允许访问相册" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"设置", nil];
alert.tag = 1;
[alert show];
} else {
[[TZImageManager manager] getCameraRollAlbum:NO allowPickingImage:YES completion:^(TZAlbumModel *model) {
[[TZImageManager manager] getAssetsFromFetchResult:model.result allowPickingVideo:NO allowPickingImage:YES completion:^(NSArray<TZAssetModel *> *models) {
[tzImagePickerVc hideProgressHUD];
......@@ -258,6 +265,7 @@
[_collectionView reloadData];
}];
}];
}
}];
}
}
......@@ -291,7 +299,11 @@
if (iOS8Later) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
} 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