先看看相簿的结构
接下来我们看看相册的函数
// Adds a photo to the saved photos album. The optional completionSelector should have the form:
// - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;
void UIImageWriteToSavedPhotosAlbum(UIImage *image, __nullable id completionTarget, __nullable SEL completionSelector, void * __nullable contextInfo)
#ForExample:
UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
## 上面的函数执行完会调用下面的方法因为@selector的原因
/**
* 通过UIImageWriteToSavedPhotosAlbum函数写入图片完毕后就会调用这个方法
*
* @param image 写入的图片
* @param error 错误信息
* @param contextInfo UIImageWriteToSavedPhotosAlbum函数的最后一个参数
*/
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
if (error) {
[SVProgressHUD showErrorWithStatus:@"图片保存失败!"];
} else {
[SVProgressHUD showSuccessWithStatus:@"图片保存成功!"];
}
}
再来看看用框架的类和方法
#import
#import
#import
PHAsset ------- // 一张图片,一个资源,一段视频
PHCollection --- // 一本相簿
assetCollectionLocalIdentifier // PHAssetCollection的标识, 利用这个标识可以找到对应的PHAssetCollection对象(相簿对象)
assetLocalIdentifier // PHAsset的标识, 利用这个标识可以找到对应的PHAsset对象(图片对象)
// 如果想对"相册"进行修改(增删改), 那么修改代码必须放在
[PHPhotoLibrary sharedPhotoLibrary]的performChanges方法的block中
保存图片到相机中的具体步骤
// 0 . 判断授权状态
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
/*
PHAuthorizationStatusNotDetermined, 用户还没有做出选择
PHAuthorizationStatusDenied, 用户拒绝当前应用访问相册(用户当初点击了"不允许")
PHAuthorizationStatusAuthorized 用户允许当前应用访问相册(用户当初点击了"好")
PHAuthorizationStatusRestricted, 因为家长控制, 导致应用无法方法相册(跟用户的选择没有关系)
*/
// 1 .保存图片到"相机胶卷中"
// 2. 创建相簿
// 3. 添加相机胶卷中的图片到新建的相簿中
// 如果想对"相册"进行修改(增删改), 那么修改代码必须放在[PHPhotoLibrary sharedPhotoLibrary]的performChanges方法的block中
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
// 1.保存图片A到"相机胶卷"中
// 创建图片的请求
assetLocalIdentifier = [PHAssetCreationRequest creationRequestForAssetFromImage:self.imageView.image].placeholderForCreatedAsset.localIdentifier;
} completionHandler:^(BOOL success, NSError * _Nullable error) {
if (success == NO) {
XMGLog(@"保存[图片]到[相机胶卷]中失败!失败信息-%@", error);
return;
}
// 获得曾经创建过的相簿
PHAssetCollection *createdAssetCollection = [self createdAssetCollection];
if (createdAssetCollection) { // 曾经创建过相簿
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
// 3.添加"相机胶卷"中的图片A到"相簿"D中
// 获得图片
PHAsset *asset = [PHAsset fetchAssetsWithLocalIdentifiers:@[assetLocalIdentifier] options:nil].lastObject;
// 添加图片到相簿中的请求
PHAssetCollectionChangeRequest *request = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:createdAssetCollection];
// 添加图片到相簿
[request addAssets:@[asset]];
} completionHandler:^(BOOL success, NSError * _Nullable error) {
if (success == NO) {
XMGLog(@"添加[图片]到[相簿]失败!失败信息-%@", error);
} else {
XMGLog(@"成功添加[图片]到[相簿]!");
}
}];
}
else { // 没有创建过相簿
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
// 2.创建"相簿"D
// 创建相簿的请求
assetCollectionLocalIdentifier = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:XMGAssetCollectionTitle].placeholderForCreatedAssetCollection.localIdentifier;
} completionHandler:^(BOOL success, NSError * _Nullable error) {
if (success == NO) {
XMGLog(@"保存相簿失败!失败信息-%@", error);
return;
}
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
// 3.添加"相机胶卷"中的图片A到新建的"相簿"D中
// 获得相簿
PHAssetCollection *assetCollection = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[assetCollectionLocalIdentifier] options:nil].lastObject;
// 获得图片
PHAsset *asset = [PHAsset fetchAssetsWithLocalIdentifiers:@[assetLocalIdentifier] options:nil].lastObject;
// 添加图片到相簿中的请求
PHAssetCollectionChangeRequest *request = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:assetCollection];
// 添加图片到相簿
[request addAssets:@[asset]];
} completionHandler:^(BOOL success, NSError * _Nullable error) {
if (success == NO) {
XMGLog(@"添加[图片]到[相簿]失败!失败信息-%@", error);
} else {
XMGLog(@"成功添加[图片]到[相簿]!");
}
}];
}];
}
}];
/**
* 获得曾经创建过的相簿
*/
- (PHAssetCollection *)createdAssetCollection
{
PHFetchResult *assetCollections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
for (PHAssetCollection *assetCollection in assetCollections) {
if ([assetCollection.localizedTitle isEqualToString:XMGAssetCollectionTitle]) {
return assetCollection;
}
}
return nil;
}