PHPotoLibrary
- 授權狀態(tài)
// 返回授權狀態(tài)
+ (PHAuthorizationStatus)authorizationStatus;
- 請求授權
// 如果授權狀態(tài)是 PHAuthorizationStatusNotDetermined 系統(tǒng)就會向用戶發(fā)起打授權請求
+ (void)requestAuthorization:(void (^)(PHAuthorizationStatus status))handler;
- 授權狀態(tài)
typedef enum PHAuthorizationStatus : NSInteger {
PHAuthorizationStatusNotDetermined = 0, // 用戶尚未允許或拒絕授權
PHAuthorizationStatusRestricted, // 應用程序未被授權訪問的照片庫,并且用戶不能授予這些權限辐赞,在iphone上測試未出現(xiàn)這種情況颓鲜,不確定是否針對mac應用
PHAuthorizationStatusDenied, // 拒絕
PHAuthorizationStatusAuthorized // 授權
} PHAuthorizationStatus;
綜合實例:
37D29F42-2B55-4342-8FD9-99036EA74C01.png
在沒確定授權的情況下狸臣,requestAuthorization 會自動發(fā)起授權請求顽耳。
- PHPotoLibrary 單例
// 返回一個單例對象
+ (PHPhotoLibrary *)sharedPhotoLibrary;
- 異步修改 Photos library
- (void)performChanges:(dispatch_block_t)changeBlock completionHandler:(void (^)(BOOL success, NSError *error))completionHandler;
- 同步修改 Photos library
- (BOOL)performChangesAndWait:(dispatch_block_t)changeBlock error:(NSError * _Nullable *)error;
- 注冊改變的監(jiān)聽
// 注冊一個對象來監(jiān)聽 photo library 的改變
- (void)registerChangeObserver:(id<PHPhotoLibraryChangeObserver>)observer;
- 注銷改變的監(jiān)聽
// 注銷一個監(jiān)聽 photo library 改變的對象
- (void)unregisterChangeObserver:(id<PHPhotoLibraryChangeObserver>)observer;
綜合實例:
- (void)AsyncAddAlbumWithName:(NSString *)name {
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
[PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:name];
} completionHandler:^(BOOL success, NSError *error) {
if (success) {
NSLog(@"異步創(chuàng)建相冊:%@成功", name);
} else {
NSLog(@"異步創(chuàng)建相冊:%@失敗", name);
}
}];
}
- (BOOL)SyncAddAlbumWithName:(NSString *)name {
return [[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
[PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:name];
} error:nil];;
}
Demo 已經(jīng)覆蓋上面所有接口腾么,已經(jīng)上傳 github亥揖,詳細查看 Demo 內容珊擂。 后面將繼續(xù)分享學習關于 Photos 和 PhotosUI 的內容圣勒,目標的覆蓋所有接口,并開發(fā)一款和 photo 相關的應用摧扇,但這都是后話了圣贸,歡迎關注我~~