IOS把图片保存到系统相册

```

一.保存到系统相册    1.调用系统的方法,必须用系统的方法    UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(image:didFinshSavingWithError:contextInfo:), nil);    2.调用方法,发挥保存是否成功的信息    -(void)image:(UIImage *)image didFinshSavingWithError:(NSError *)error contextInfo:(void*)contextInfo{    if (error) {        [SVProgressHUD showErrorWithStatus:@"保存失败"];    }else    {        [SVProgressHUD showSuccessWithStatus:@"保存成功"];    }}```----------------````二.保存到自定义相册    1.苹果保存图片到自己相册业务逻辑:把图片保存到系统相册中,在从系统相册中拷贝一份到自己相册.    2.使用苹果的框架.

3. photos框架:专门处理相册

4.pgotos框架基本类

PHPhotoLibrary:相簿(所有相册集合)

PHAsset:图片(资源文件)

PHAssetCollection:相册(所有图片集合)

PHAssetCreationRequest:创建,删除,修改图片,必须要有这个类

PHAssetCollectionChangeRequest:创建,删除,修改相册,必须要创建这个类

photos框架使用规则:要操作相册或者相片,必须发送请求

````

-------

```

三. photos框架的基本使用

//搜索自定义相册

- (PHAssetCollection *)fetchAlbum

{

//    获取系统相册

PHFetchResult *resul = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];

//    遍历系统相册

for (PHAssetCollection *assetCollection in resul) {

if ([assetCollection.localizedTitle isEqualToString:XMGAlbumTitle]) {

return assetCollection;

}

}

return nil;

}

//保存图片方法

-(void)savePhoto

{

[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{

//    判断是否已经存在相册,如果已经存在,保存到之前相册,无则创建相册

PHAssetCollection *album = [self fetchAlbum];

// 创建相册请求类

PHAssetCollectionChangeRequest *assetCollectionChangeRequest;

if (album) {

// 获取相册

assetCollectionChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:album];

}else{

//    创建自己的相册(相册请求类:创建相册)

PHAssetCollectionChangeRequest *assetCollectionChangeRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:XMGAlbumTitle];

}

//    创建图片请求类

PHAssetCreationRequest *assetCreationRequest = [PHAssetCreationRequest creationRequestForAssetFromImage:self.imageView.image];

//    添加图片到相册(相册请求类)

[assetCollectionChangeRequest addAssets:@[assetCreationRequest.placeholderForCreatedAsset]];

} completionHandler:^(BOOL success, NSError * _Nullable error) {

if (success) {

[SVProgressHUD showSuccessWithStatus:@"保存成功"];

}else{

[SVProgressHUD showErrorWithStatus:@"保存失败"];

}

}];

}

```

---------------

```

四.photos框架的用户授权问题

获取用户访问相册状态

PHAuthorizationStatusNotDetermined : 不决定

PHAuthorizationStatusRestricted :    手机被家长控制,不能修改状态

PHAuthorizationStatusDenied    :拒绝

PHAuthorizationStatusAuthorized    :允许

---------------------------------------------------------

switch ([PHPhotoLibrary authorizationStatus]) {

case PHAuthorizationStatusNotDetermined:

{

// 不确定 => 弹出框,询问用户是否允许访问系统相册

[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {

if (status == PHAuthorizationStatusAuthorized) {

// 保存图片

[self savePhoto];

}

}];

break;

}

case PHAuthorizationStatusAuthorized:

{

// 授权 => 保存图片

[self savePhoto];

break;

}

default:

{

// 拒绝 => 弹出框,告诉用户,允许访问相册

[SVProgressHUD showInfoWithStatus:@"设置 -> 百思不得姐 -> 打开允许访问相册开关"];

}

}

;

```

你可能感兴趣的:(IOS把图片保存到系统相册)