ALAssetsLibrary---保存照片到指定相册

经常我们会把程序中的照片保存到指定相册中

保存到系统相册

 ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
 [assetsLibrary writeImageDataToSavedPhotosAlbum:imageData metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
            
        }];

系统还提供了另外一些API

// With a UIImage, the API user can use -[UIImage CGImage] to get a CGImageRef, and cast -[UIImage imageOrientation] to ALAssetOrientation.

- (void)writeImageToSavedPhotosAlbum:(CGImageRef)imageRef orientation:(ALAssetOrientation)orientation completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)completionBlock;


// The API user will have to specify the orientation key in the metadata dictionary to preserve the orientation of the image

- (void)writeImageToSavedPhotosAlbum:(CGImageRef)imageRef metadata:(NSDictionary *)metadata completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)completionBlock


// If there is a conflict between the metadata in the image data and the metadata dictionary, the image data metadata values will be overwritten

- (void)writeImageDataToSavedPhotosAlbum:(NSData *)imageData metadata:(NSDictionary *)metadata completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)completionBlock


保存到新建相册

 ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
 [assetsLibrary writeImageDataToSavedPhotosAlbum:imageData metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
            if (error!=nil) {
                return;
            }
            // 先枚举所有的相册组
            [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupLibrary usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
                // 如果已经有这个相册
                if ([NewAlbum compare: [group valueForProperty:ALAssetsGroupPropertyName]]==NSOrderedSame) {
                    [assetsLibrary assetForURL:assetURL resultBlock:^(ALAsset *asset) {
                        [group addAsset:asset];
                    } failureBlock:^(NSError *error) {
                        
                    }];
                }
                else {
                    // 如果没有这个相册
                    __weak ALAssetsLibrary *weakLibrary = assetsLibrary;
                    [weakLibrary addAssetsGroupAlbumWithName:NewAlbum resultBlock:^(ALAssetsGroup *group) {
                        // 保存到相册
                        [assetsLibrary assetForURL:assetURL resultBlock:^(ALAsset *asset) {
                             [group addAsset:asset];
                        } failureBlock:^(NSError *error) {
                            
                        }];
                    } failureBlock:^(NSError *error) {
                        
                    }];
                }
            } failureBlock:^(NSError *error) {
                
            }];
        }];
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值