如何使用iOS 14中的PHAuthorizationStatusLimited [英] How to use PHAuthorizationStatusLimited in iOS 14

查看:1719
本文介绍了如何使用iOS 14中的PHAuthorizationStatusLimited的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了获取照片的creationDate,请在显示PHPickerViewController之前使用requestAuthorizationForAccessLevel.

In order to fetch photo's creationDate, so use requestAuthorizationForAccessLevel before show PHPickerViewController.

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

尽管状态为.limited,但iOS 14仍显示所有图像.

although status is .limited, but iOS 14 still display all images.

如何使用PHPickerViewController仅获取有限的照片?

How can i get only limited photos with PHPickerViewController?

推荐答案

因此,iOS 14中发生了一些更改,让我们一步一步看

So a couple of things got changed in iOS 14, let's see step by step

1.如何读取PHPhotoLibrary访问权限状态

let status = PHPhotoLibrary.authorizationStatus()

let status = PHPhotoLibrary.authorizationStatus(for: .readWrite)

2.如何请求PHPhotoLibrary访问权限

PHPhotoLibrary.requestAuthorization { status in
 //your code               
 }

PHPhotoLibrary.requestAuthorization(for: .readWrite) { status in
      switch status {
          case .limited:
               print("limited access granted")
                
          default:
               print("denied, .restricted ,.authorized")
                
      }
  }

在用户授予您有限权限的情况下,您有责任像下面的示例代码一样显示图库

It is your responsibility to show gallery like below sample code in case of user granted you limited permission

if status == .limited {
     PHPhotoLibrary.shared().presentLimitedLibraryPicker(from: self)
}

当您呈现LimitedLibraryPicker时,从上一个会话中选择的图像将被标记为选中,同时屏幕上方还会显示一条消息-"选择更多照片或取消选择以删除访问权限"

When you presentLimitedLibraryPicker the selected images from the previous session would be already marked check, along with a message on top of screen- "Select more photos or deselect to remove access"

在用户授予您有限访问权限的情况下,您仍然可以使用UIImagePickerController或BSImagePicker之类的第三方库来展示普通画廊,即使您可以选择并导入到您的应用中,也将显示包含所有图片的画廊,但是在Xcode 12控制台中它会显示以下警告

In-case the user granted you limited access still you present the normal gallery using UIImagePickerController or a third party library like BSImagePicker, a gallery with all pictures would be shown even you can select and import into your app but in Xcode 12 console it will show warnings as below

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}

这篇关于如何使用iOS 14中的PHAuthorizationStatusLimited的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆