保存到相册的方法
//保存图片
- (IBAction)downLoadClick:(id)sender {
// 一、保存图片时候调用(保存到系统相册)
// UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
// 二、保存图片到自定义相册。photos框架 #import <Photos/Photos.h>
/*
PHAuthorizationStatusNotDetermined 不确定
PHAuthorizationStatusRestricted 家长模式,拒绝
PHAuthorizationStatusDenied, 拒绝
PHAuthorizationStatusAuthorized 授权
*/
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus] ;
if (status == PHAuthorizationStatusNotDetermined) { //不确定
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
if (status == PHAuthorizationStatusAuthorized) {
[self savePhoto] ;
}
}] ;
}else if (status == PHAuthorizationStatusAuthorized){
[self savePhoto] ;
}else{
//拒绝
//提示用户去打开权限
[SVProgressHUD showWithStatus:@"应用未打开权限,请去设置中打开权限!"] ;
}
}
获取相册
//获取之前的相册
-(PHAssetCollection *)fetchAssetCollection:(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 ;
}
-(void)savePhoto{
// 二、保存图片到自定义相册。photos框架 <Photos/Photos.h>
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
//判断之前有没有创建有没有去创建
PHAssetCollection *assetCollection = [self fetchAssetCollection:@"百思不得姐"] ;
PHAssetCollectionChangeRequest *assetCollectionChangeRequest ;
if (assetCollection) {
//已有相册
assetCollectionChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:assetCollection] ;
}else{//没有相册
//1.创建自定义相册assetCollectionChangeRequest assetCollectionChangeRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:@"百思不得姐"] ;
}
//2.添加图片到系统相册
PHAssetChangeRequest *assetChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:self.imageView.image] ;
//3.拷贝图片到自定义相册(把图片的地址给自定义相册)
PHObjectPlaceholder *placeholder = [assetChangeRequest placeholderForCreatedAsset] ;
[assetCollectionChangeRequest addAssets:@[placeholder]] ;
} completionHandler:^(BOOL success, NSError * _Nullable error) {
if (error) {
[SVProgressHUD showErrorWithStatus:@"保存失败!"];
}else{
[SVProgressHUD showSuccessWithStatus:@"保存成功"];
}
}] ;
}
保存图片时候调用(保存到系统相册)
//- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
//{
//
// if (error) {
//
// [SVProgressHUD showErrorWithStatus:@"保存失败!"];
//
// }else{
//
// [SVProgressHUD showSuccessWithStatus:@"保存成功"];
//
// }
//}