PHPotoLibrary
- 授权状态
// 返回授权状态
+ (PHAuthorizationStatus)authorizationStatus;
- 请求授权
// 如果授权状态是 PHAuthorizationStatusNotDetermined 系统就会向用户发起打授权请求
+ (void)requestAuthorization:(void (^)(PHAuthorizationStatus status))handler;
- 授权状态
typedef enum PHAuthorizationStatus : NSInteger {
PHAuthorizationStatusNotDetermined = 0, // 用户尚未允许或拒绝授权
PHAuthorizationStatusRestricted, // 应用程序未被授权访问的照片库,并且用户不能授予这些权限,在iphone上测试未出现这种情况,不确定是否针对mac应用
PHAuthorizationStatusDenied, // 拒绝
PHAuthorizationStatusAuthorized // 授权
} PHAuthorizationStatus;
综合实例:
37D29F42-2B55-4342-8FD9-99036EA74C01.png在没确定授权的情况下,requestAuthorization 会自动发起授权请求。
- 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;
- 注册改变的监听
// 注册一个对象来监听 photo library 的改变
- (void)registerChangeObserver:(id<PHPhotoLibraryChangeObserver>)observer;
- 注销改变的监听
// 注销一个监听 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(@"异步创建相册:%@成功", name);
} else {
NSLog(@"异步创建相册:%@失败", name);
}
}];
}
- (BOOL)SyncAddAlbumWithName:(NSString *)name {
return [[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
[PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:name];
} error:nil];;
}
Demo 已经覆盖上面所有接口,已经上传 github,详细查看 Demo 内容。 后面将继续分享学习关于 Photos 和 PhotosUI 的内容,目标的覆盖所有接口,并开发一款和 photo 相关的应用,但这都是后话了,欢迎关注我~~
网友评论