iOS——存储图片到自定义相册

1、将图片存储到系统相册“相机胶卷”

/* 将图片保存到系统相册(相机胶卷) */
+(PHObjectPlaceholder *)saveToCameraRollAlbumnWithImage:(UIImage *)image {
    
    __block PHObjectPlaceholder *placeHolder = nil;
    NSError *err;
    
    [[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
        placeHolder = [PHAssetChangeRequest creationRequestForAssetFromImage:image].placeholderForCreatedAsset;
    } error:(&err)];
    
    if (err) {
        NSLog(@"保存图片失败");
    }
    
    return placeHolder;
}

2、根据应用名称创建自定义相册

/* 根据APP的名称创建一个自定义相册 */
+(PHAssetCollection *)createCustomAssetCollection {
    /* 1、获取app的名称 */
    NSString *collectionTitle = [NSBundle mainBundle].infoDictionary[(__bridge_transfer NSString *)kCFBundleNameKey];
    NSError *error = nil;
    
    // 查找 app 中是否有该相册 如果已经有了 就不再创建
    /**
     *     参数一 枚举:
     *     PHAssetCollectionTypeAlbum      = 1, 用户自定义相册
     *     PHAssetCollectionTypeSmartAlbum = 2, 系统相册
     *     PHAssetCollectionTypeMoment     = 3, 按时间排序的相册
     *
     *     参数二 枚举:PHAssetCollectionSubtype
     *     参数二的枚举有非常多,但是可以根据识别单词来找出我们想要的。
     *     比如:PHAssetCollectionTypeSmartAlbum 系统相册 PHAssetCollectionSubtypeSmartAlbumUserLibrary 用户相册 就能获取到相机胶卷
     *     PHAssetCollectionSubtypeAlbumRegular 常规相册
     */
    PHFetchResult<PHAssetCollection *> *result = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
    
    for (PHAssetCollection *collection in result) {
        if ([collection.localizedTitle isEqualToString:collectionTitle]) {
            return collection;
        }
    }
    
    __block NSString *createdCustomAssetCollectionIdentifier = nil;
    // 创建和 app 名称一样的 相册
    /**
     * 注意:这个方法只是告诉 photos 我要创建一个相册,并没有真的创建
     *      必须等到 performChangesAndWait block 执行完毕后才会
     *      真的创建相册。
     */
    [[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
        PHAssetCollectionChangeRequest *collectionChangeRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:collectionTitle];
        /**
         * collectionChangeRequest 即使我们告诉 photos 要创建相册,但是此时还没有
         * 创建相册,因此现在我们并不能拿到所创建的相册,我们的需求是:将图片保存到
         * 自定义的相册中,因此我们需要拿到自己创建的相册,从头文件可以看出,collectionChangeRequest
         * 中有一个占位相册,placeholderForCreatedAssetCollection ,这个占位相册
         * 虽然不是我们所创建的,但是其 identifier 和我们所创建的自定义相册的 identifier
         * 是相同的。所以想要拿到我们自定义的相册,必须保存这个 identifier,等 photos app
         * 创建完成后通过 identifier 来拿到我们自定义的相册
         */
        createdCustomAssetCollectionIdentifier = collectionChangeRequest.placeholderForCreatedAssetCollection.localIdentifier;
    } error:&error];
    
    // 这里 block 结束了,因此相册也创建完毕了
    if (error) {
        NSLog(@"创建相册失败");
    }
    return [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[createdCustomAssetCollectionIdentifier] options:nil].firstObject;
}


3、将图片保存到自定义相册(调用上面两个方法)

+(void)saveToAlbumnWithImage:(UIImage *)image completion:(void (^)())complete {
    /* 获取自定义的相册 */
    PHAssetCollection *customCollection = [self createCustomAssetCollection];
    
    // 将图片保存到自定义相册
    /**
     * 必须通过中间类,PHAssetCollectionChangeRequest 来完成
     * 步骤:1.首先根据相册获取 PHAssetCollectionChangeRequest 对象
     *      2.然后根据 PHAssetCollectionChangeRequest 来添加图片
     * 这一步的实现有两个思路:1.通过上面的占位 asset 的标识来获取 相机胶卷中的 asset
     *                       然后,将 asset 添加到 request 中
     *                     2.直接将 占位 asset 添加到 request 中去也是可行的
     */
    PHObjectPlaceholder *placeholder = [self saveToCameraRollAlbumnWithImage:image];
    NSError *error = nil;
    
    [[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
        PHAssetCollectionChangeRequest *request = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:customCollection];
        [request insertAssets:@[placeholder] atIndexes:[NSIndexSet indexSetWithIndex:0]];
    } error:&error];
    
//    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
//        
//    } completionHandler:^(BOOL success, NSError * _Nullable error) {
//        
//    }];
    
    if (error) {
        NSLog(@"保存失败");
    } else {
        complete();
    }
    
}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值