将照片保存到iOS 8中的自定义相册

时间:2015-05-14 23:28:34

标签: ios objective-c uiimage photos

我需要一些帮助,我有一个方法可以在iOS 8中保存UIImage到相机胶卷而没有问题。方法如下

[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
    [PHAssetChangeRequest creationRequestForAssetFromImage:image];
}completionHandler:^(BOOL success, NSError *error) {
    if(success){
        NSLog(@"worked");
    }else{
        NSLog(@"Error: %@", error);

    }
}];

我需要调整该代码,以便图像而不是将UIImage保存到相机胶卷,它保存到名为“MyAlbum”的自定义相册

我正在使用Photos.framework

4 个答案:

答案 0 :(得分:17)

首先需要检查相册是否存在获取请求,然后将图像添加到相册或创建相册,然后添加图像。

echo >&2 "Installing Homebrew"
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

答案 1 :(得分:7)

创建名为" MyAlbum"的新专辑在将资产添加到相册之前。

    // Create new album.
    __block PHObjectPlaceholder *albumPlaceholder;
    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
        PHAssetCollectionChangeRequest *changeRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:title];
        albumPlaceholder = changeRequest.placeholderForCreatedAssetCollection;
    } completionHandler:^(BOOL success, NSError *error) {
        if (success) {
            PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[albumPlaceholder.localIdentifier] options:nil];
            PHAssetCollection *assetCollection = fetchResult.firstObject;

            // Add it to the photo library
            [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
                PHAssetChangeRequest *assetChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];

                PHAssetCollectionChangeRequest *assetCollectionChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:assetCollection];
                [assetCollectionChangeRequest addAssets:@[[assetChangeRequest placeholderForCreatedAsset]]];
            } completionHandler:^(BOOL success, NSError *error) {
                if (!success) {
                    NSLog(@"Error creating asset: %@", error);
                }
            }];
        } else {
            NSLog(@"Error creating album: %@", error);
        }
    }];

答案 2 :(得分:0)

在执行更改块中,指定请求的assetCollection

let request = PHAssetCollectionChangeRequest.creationRequestForAssetCollectionWithTitle(-YOUR ALBUM NAME-)

答案 3 :(得分:0)

检查相册是否已存在。

log_in