iOS 弹出系统相册权限弹窗

 使用 PHPhotoLibrary 库

#import <Photos/PHPhotoLibrary.h>

- (void)clickPhotoLibrary
{
    PHAuthorizationStatus authorization = [PHPhotoLibrary authorizationStatus];
    if (authorization == PHAuthorizationStatusNotDetermined) {
        if (@available(iOS 14, *)) {
            [PHPhotoLibrary requestAuthorizationForAccessLevel:PHAccessLevelReadWrite handler:^(PHAuthorizationStatus status) {
                [self reloadData];
            }];
        } else {
            [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
                [self reloadData];
            }];
        }
    } else {
        [self goSyemtmSetting];
    }
}

一般情况下 判断是否获取相册权限,我们可以简单的使用下面这个方法

```

- (BOOL)havePhotoLibraryPermission

{

    PHAuthorizationStatus authorization = [PHPhotoLibrary authorizationStatus];

    if (authorization == PHAuthorizationStatusAuthorized) {

        return YES;

    }

return NO

```

下载图片并保存到本地

           [self downloadImageWithURL:[NSURL URLWithString:downloadUrl] complete:^(UIImage *image, BOOL finish) {
                if (image && finish) {
                    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
                        if (status == PHAuthorizationStatusRestricted || status == PHAuthorizationStatusDenied) {
                            dispatch_async(dispatch_get_main_queue(), ^{
                                [weakSelf showAlertViewContollerWithTitle:@"无法保存图片" message:@"请前往系统设置打开照片权限,该权限用于保存图片" cancleName:@"取消" sureName:@"去设置" WithSureAction:^{
                                    openSystemSetting();
                                } cancleAction:^{
                                    
                                }];
                            });
                        } else if (status == PHAuthorizationStatusNotDetermined) {
                            if (@available(iOS 14, *)) {
                                [PHPhotoLibrary requestAuthorizationForAccessLevel:PHAccessLevelReadWrite handler:^(PHAuthorizationStatus status) {
                                    if (status ==  PHAuthorizationStatusAuthorized ||
                                        status == PHAuthorizationStatusLimited) {
                                        UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
                                    }
                                }];
                            } else {
                                [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
                                    if (status ==  PHAuthorizationStatusAuthorized) {
                                        UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
                                    }
                                }];
                            }
                        } else{
                            dispatch_async(dispatch_get_main_queue(), ^{
                                UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
                            });
                        }
                    }];
                }
            }];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值