1、C语言方法
在需要保存图片的代码中调用下列方法即可
UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
注意:在上诉select方法中必须要按规定调用下列方法才可以,不可以自定义方法,否则会编译报错
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
if (error) {
[SVProgressHUD showErrorWithStatus:@"图片保存失败!!!"];
} else {
[SVProgressHUD showSuccessWithStatus:@"图片保存成功!"];
}
}
2、Photos库
PHAsset:一张相片或者一个视频
PHAssetCollection:一个相册
相片和相册的操作共有4中情况:增删改查,其中增删改归纳为1类,查归纳为1类;
所有的增删改操作都需要在-[PHPhotoLibrary performChanges:] 或者 -[PHPhotoLibrary performChangesAndWait:] block方法中执行,否者会编译报错
用户保存一张图片到自定义相册代码示例
// 获取保存到相册的图片
PHFetchResult<PHAsset *> *createdAssets = [self createdAssets];
if (createdAssets == nil) {
[SVProgressHUD showErrorWithStatus:@"保存图片失败!!!"];
return;
}
// 查找自定义相册
PHAssetCollection *collect = [self createdCollection];
if (collect == nil) {
[SVProgressHUD showErrorWithStatus:@"自定义相册创建失败"];
return;
}
// 保存图片到自定义相册
[[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
PHAssetCollectionChangeRequest *request = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:collect];
[request insertAssets:createdAssets atIndexes:[NSIndexSet indexSetWithIndex:0]];
} error:&error];
if (error) {
[SVProgressHUD showErrorWithStatus:@"保存图片到自定义相册失败"];
} else {
[SVProgressHUD showSuccessWithStatus:@"保存图片到自定义相册成功"];
}
查找自定义相册方法,如果自定义相册不存在,则创建一个自定义相册并返回
- (PHAssetCollection *)createdCollection
{
// 取出软件名称
NSString *title = [NSBundle mainBundle].infoDictionary[(NSString *)kCFBundleNameKey];
// 获取相册集
PHFetchResult<PHAssetCollection *> *collects = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
// 遍历相册集,查找自定义相册
for (PHAssetCollection *collect in collects) {
if ([collect.localizedTitle isEqualToString:title]) {
return collect;
}
}
NSError *error = nil;
__block NSString * createdcollectID = nil;
// 遍历相册集没有找到自定义相册,创建一个自定义相册
[[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
// 创建一个自定义相册,并返回Identifier
createdcollectID = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:title].placeholderForCreatedAssetCollection.localIdentifier;
} error:&error];
if (error) {
return nil;
} else {
[SVProgressHUD showSuccessWithStatus:@"相册创建成功"];
}
// 通过相册的Identifier快速遍历相册集并返回相册
return [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[createdcollectID] options:nil].firstObject;
}
获取保存到相册的图片方法,保存图片到相册并返回图片
- (PHFetchResult<PHAsset *> *)createdAssets
{
NSError *error = nil;
__block NSString * assetID = nil;
[[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
// 保存图片,并获取保存图片的Identifier
assetID = [PHAssetChangeRequest creationRequestForAssetFromImage:self.imageView.image].placeholderForCreatedAsset.localIdentifier;
} error:&error];
if (error) {
return nil;
}
// 通过图片的Identifier快速遍历图片并返回图片
return [PHAsset fetchAssetsWithLocalIdentifiers:@[assetID] options:nil];
}
注意事项,在保存图片的时候需要确定是否有判断APP是否有权限访问相册,可调用以下方法根据返回状态进行判断
(void)requestAuthorization:(void(^)(PHAuthorizationStatus status))handler