保存图片到相簿中

作品链接:
http://www.jianshu.com/users/1e0f5e6f73f6/top_articles

  • 需要导入第三方框架
#import 
#import 

1.设置用户访问相簿的权限

- (IBAction)save {
    
    /*
     PHAuthorizationStatusNotDetermined,     用户还没有做出选择
     PHAuthorizationStatusDenied,            用户拒绝当前应用访问相册(用户当初点击了"不允许")
     PHAuthorizationStatusAuthorized         用户允许当前应用访问相册(用户当初点击了"好")
     PHAuthorizationStatusRestricted,        因为家长控制, 导致应用无法方法相册(跟用户的选择没有关系)
     */

    // 判断授权状态
    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
    
    if (status == PHAuthorizationStatusRestricted) {
        [SVProgressHUD showErrorWithStatus:@"因为系统原因,无法访问相册"];
    } else if (status == PHAuthorizationStatusDenied){
        // 用户拒绝当前应用访问相册(用户当初点击了"不允许")
        PHLog(@"提醒用户去[设置-隐私-照片-xxx]打开访问开关");
    } else if (status == PHAuthorizationStatusAuthorized){
        //用户允许当前应用访问相册  用户当初点击了好
        [self saveImage];
    } else if (status == PHAuthorizationStatusNotDetermined){
        
        // 用户还没有做出选择
        // 弹框请求用户授权
        [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
            if (status == PHAuthorizationStatusAuthorized) {
                [self saveImage];
            }
        }];
     }
}

2.保护图片

- (void)saveImage
{
    // PHAsset : 一个资源, 比如一张图片\一段视频
    // PHAssetCollection : 一个相簿
    
    // PHAssetCollection的标识,利用这个标识可以找到对应的PHAssetCollection的对象(相簿对象)
    __block NSString *assetCollectionLocalIdentifier = nil;
    __block NSString *assetLocalIdentifier = nil;
    // PHAsset的标识,利用这个标识可以找到对应的PHAsset对象 即图片对象
    
    // 如果想对"相册"进行修改(增删改), 那么修改代码必须放在[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) {
            PHLog(@"保存【图片】到【相机胶卷】中失败!失败信息-%@",error);
            return ;
        }
        
        // 获得曾经创建过的相簿
        PHAssetCollection *createdAssetCollection = [self createdAssetCollection];
        if (createdAssetCollection) {
            // 3.添加【相机胶卷】中的图片A到【相簿】D中
           [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
                // 获得图片
               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) {
                   PHLog(@"添加[图片]到[相簿]失败!失败信息-%@",error);
               } else{
                   PHLog(@"成功添加[图片]到[相簿]!");
               }
           }];
           
        } else{
            // 没有创建过相簿
            [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
                //2.创建"相簿"D
                // 创建相簿的请求
                
                assetCollectionLocalIdentifier = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:PPHAssetCollectionTitle].placeholderForCreatedAssetCollection.localIdentifier;
                
            } completionHandler:^(BOOL success, NSError * _Nullable error) {
                if (!success) {
                    PHLog(@"保存相簿失败!失败信息-%@",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) {
                        PHLog(@"添加[图片]到[相簿]失败!失败信息-%@",error);
                    } else{
                        PHLog(@"成功添加[图片]到[相簿]!");
                    }
                }];
            }];
        }
    }];
}

3.创建相册

- (PHAssetCollection *)createdAssetCollection
{
    PHFetchResult *assetCollections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
    
    for (PHAssetCollection *assetCollection in assetCollections) {
        if ([assetCollection.localizedTitle isEqualToString:PPHAssetCollectionTitle]) {
            return assetCollection;
        }
    }
    return nil;
}

你可能感兴趣的:(保存图片到相簿中)