Mr.*_*ity 6 ios14 phpickerviewcontroller
为了获取照片的创建日期,所以在显示 PHPickerViewController 之前使用 requestAuthorizationForAccessLevel。
PHAccessLevel level = PHAccessLevelReadWrite;
[PHPhotoLibrary requestAuthorizationForAccessLevel:level handler:^(PHAuthorizationStatus status) {
if (status == PHAuthorizationStatusLimited || status == PHAuthorizationStatusAuthorized) {
dispatch_async(dispatch_get_main_queue(), ^{
PHPickerConfiguration *configuration = [[PHPickerConfiguration alloc] initWithPhotoLibrary:[PHPhotoLibrary sharedPhotoLibrary]];
configuration.filter = [PHPickerFilter imagesFilter];
configuration.selectionLimit = 1;
PHPickerViewController *picker = [[PHPickerViewController alloc] initWithConfiguration:configuration];
picker.delegate = self;
[self showViewController:picker sender:nil];
});
}
}];
Run Code Online (Sandbox Code Playgroud)
尽管状态为 .limited,但 iOS 14 仍然显示所有图像。
如何使用 PHPickerViewController 只获取有限的照片?
因此,iOS 14 中发生了一些变化,让我们一步一步来看看
1.如何读取PHPhotoLibrary访问权限状态
老的
let status = PHPhotoLibrary.authorizationStatus()
Run Code Online (Sandbox Code Playgroud)
新的
let status = PHPhotoLibrary.authorizationStatus(for: .readWrite)
Run Code Online (Sandbox Code Playgroud)
2.如何申请PHPhotoLibrary访问权限
老的
PHPhotoLibrary.requestAuthorization { status in
//your code
}
Run Code Online (Sandbox Code Playgroud)
新的
PHPhotoLibrary.requestAuthorization(for: .readWrite) { status in
switch status {
case .limited:
print("limited access granted")
default:
print("denied, .restricted ,.authorized")
}
}
Run Code Online (Sandbox Code Playgroud)
如果用户授予您有限的权限,您有责任像下面的示例代码一样显示图库
if status == .limited {
PHPhotoLibrary.shared().presentLimitedLibraryPicker(from: self)
}
Run Code Online (Sandbox Code Playgroud)
当您展示LimitedLibraryPicker 时,从上一个会话中选择的图像将被标记为选中,并且屏幕顶部会显示一条消息-“选择更多照片或取消选择以删除访问权限”
如果用户授予您有限的访问权限,您仍然使用 UIImagePickerController 或第三方库(如 BSImagePicker)展示普通图库,即使您可以选择并导入到您的应用程序,也会显示包含所有图片的图库,但在 Xcode 12 控制台中它会显示警告如下
Failed to decode image
[ImageManager] Failed to get sandbox extension for url: file///filepath/5003.JPG, error: Error Domain=com.apple.photos.error Code=41008 "Invalid asset uuid for client" UserInfo={NSLocalizedDescription=Invalid asset uuid for client}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3576 次 |
| 最近记录: |