使用 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);
});
}
}];
}
}];