1.了解这个框架有哪些常用类
2.查看苹果官方文档 (1.想看框架有哪些类,Reference 2.想了解框架怎么用,Guide),想了解一个类怎么去用,可以使用option
例1如下:
/*
PHPhotoLibrary:相簿(所有相册集合)
PHAsset:图片
PHAssetCollection:相册,所有相片集合
PHAssetChangeRequest:创建,修改,删除图片
PHAssetCollectionChangeRequest:创建,修改,删除相册
*/
/*
0.显示提示框,询问用户是否允许当前App访问相册
1.每次都创建新的相册
*/
// 获取用户授权状态
/*
PHAuthorizationStatusNotDetermined = 0, 不确定
PHAuthorizationStatusRestricted, 家长控制,拒绝
PHAuthorizationStatusDenied, 拒绝
PHAuthorizationStatusAuthorized 授权
*/
#pragma mark - 获取之前相册
- (PHAssetCollection *)fetchAssetColletion:(NSString *)albumTitle
{
// 获取之前相册
PHFetchResult *result = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
for (PHAssetCollection *assetCollection in result) {
if ([assetCollection.localizedTitle isEqualToString:albumTitle]) {
return assetCollection;
}
}
return nil;
}
#pragma mark - 保存图片
- (void)savePhoto
{
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
// 判断之前有没有相册,获取之前相册
PHAssetCollection *assetCollection = [self fetchAssetColletion:XMGAlbumTitle];
PHAssetCollectionChangeRequest *assetCollectionChangeRequest;
if (assetCollection) {
// 已有相册
assetCollectionChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:assetCollection];
} else {
// 1.创建自定义相册
assetCollectionChangeRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:XMGAlbumTitle];
}
// 2.保存图片到系统相册
PHAssetChangeRequest *assetChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:_imageView.image];
// 3.把创建好图片添加到自己相册
PHObjectPlaceholder *placeholder = [assetChangeRequest placeholderForCreatedAsset];
[assetCollectionChangeRequest addAssets:@[placeholder]];
} completionHandler:^(BOOL success, NSError * _Nullable error) {
if (error) {
[SVProgressHUD showErrorWithStatus:@"保存失败"];
} else {
[SVProgressHUD showSuccessWithStatus:@"保存成功"];
}
}];
}