1.
#import <PhotosUI/PhotosUI.h>
Info.plist 中設置
“PHPhotoLibraryPreventAutomaticLimitedAccessAlert”的值為 YES 來阻止該彈窗反復彈出
2.iOS14之后獲取相冊授權狀態的方法
if (@available(iOS 14, *)) {
[PHPhotoLibrary requestAuthorizationForAccessLevel:PHAccessLevelReadWrite handler:^(PHAuthorizationStatus status) {
dispatch_async(dispatch_get_main_queue(), ^{
///用戶已授權此應用程序有限的照片庫訪問權限
if (status == PHAuthorizationStatusLimited) {
}else{
}
});
}];
}
3.第一種使用方法,和QQ一樣,推薦使用
不在 Info.plist 阻止彈窗彈出, 使用監聽獲取回調
1. 實現PHPhotoLibraryChangeObserver
@interface AddWatermarkViewController ()<PHPhotoLibraryChangeObserver>
2. 注冊監聽
- (void)viewDidLoad {
[super viewDidLoad];
[[PHPhotoLibrary sharedPhotoLibrary] registerChangeObserver:self];
}
/// PHPhotoLibrary監聽: 選擇圖片之后的回調
#pragma mark- PHPhotoLibraryChangeObserver
- (void)photoLibraryDidChange:(PHChange *)changeInstance{
///一定要在主線程刷新:這里是異步監聽
dispatch_async (dispatch_get_main_queue (), ^{
[self reloadSelectedAssetCollection:nil];
});
}
4. 注銷監聽
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[[PHPhotoLibrary sharedPhotoLibrary] unregisterChangeObserver:self];
}
2.自定義控制器,此方法選了之后去 設置中查看, 還是沒效果。。 原因待查
1. 設置代理 PHPickerViewControllerDelegate
@interface AddWatermarkViewController ()< PHPickerViewControllerDelegate>
2.手動顯示彈窗
- (void)showAlert{
@WeakObj(self);
if (@available(iOS 14, *)) {
///14之后想着授權狀態方法:異步的
[PHPhotoLibrary requestAuthorizationForAccessLevel:PHAccessLevelReadWrite handler:^(PHAuthorizationStatus status) {
dispatch_async(dispatch_get_main_queue(), ^{
@StrongObj(self);
if (status == PHAuthorizationStatusLimited) {
PHPickerConfiguration *configuration = [[PHPickerConfiguration alloc] init];
configuration.filter = [PHPickerFilter imagesFilter]; // 可配置查詢用戶相冊中文件的類型,支持三種 imagesFilter 表示 只顯示圖片
configuration.selectionLimit = 0; // 默認為1,為0時表示可多選。
PHPickerViewController *picker = [[PHPickerViewController alloc] initWithConfiguration:configuration];
picker.delegate = self;
[self presentViewController:picker animated:YES completion:nil];
}
});
}];
}
}
//點擊取消或者完成的回調
- (void)picker:(PHPickerViewController *)picker didFinishPicking:(NSArray<PHPickerResult *> *)results
API_AVAILABLE(ios(14)){
[picker dismissViewControllerAnimated:YES completion:nil];
if (!results || !results.count) {
return;
}
[self reloadSelectedAssetCollection:self.selectedAssetCollection];
NSLog(@"didFinishPicking");
}
參考:
iOS14 適配 先行者
https://www.jianshu.com/p/a0b0303962bb
iOS14 隱私適配及部分解決方案
https://www.jianshu.com/p/1803bd950b90?devtype=android
