将照片保存到iOS 8中的自定义相册 [英] Save a photo to custom album in iOS 8

查看:176
本文介绍了将照片保存到iOS 8中的自定义相册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里我需要一点帮助,我有一个方法可以将UIImage保存到相机胶卷中,而在iOS 8中则不会出现问题.该方法如下

I need a little bit of help in here, I have a method that saves an UIImage to the camera roll without problems in iOS 8. The method is the following

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

    }
}];

我需要修改该代码,以便将图像保存到名为"MyAlbum"的自定义相册中,而不是将UIImage保存到相机胶卷中.

I need to adapt that code, so that the image instead of saving the UIImage to the camera roll, it saves to a custom album named "MyAlbum"

我正在使用Photos.framework

I'm using the Photos.framework

推荐答案

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

You will first need to check that the album exists with a fetch request, and then either add the image to the album or create the album and then add the image.

#import <Photos/Photos.h>

- (void)saveToAlbum:(UIImage *)image {
    NSString *albumName = @"MyAlbum";

    void (^saveBlock)(PHAssetCollection *assetCollection) = ^void(PHAssetCollection *assetCollection) {
        [[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);
            }
        }];
    };

    PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
    fetchOptions.predicate = [NSPredicate predicateWithFormat:@"localizedTitle = %@", albumName];
    PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:fetchOptions];
    if (fetchResult.count > 0) {
        saveBlock(fetchResult.firstObject);
    } else {
        __block PHObjectPlaceholder *albumPlaceholder;
        [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
            PHAssetCollectionChangeRequest *changeRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:albumName];
            albumPlaceholder = changeRequest.placeholderForCreatedAssetCollection;

        } completionHandler:^(BOOL success, NSError *error) {
            if (success) {
                PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[albumPlaceholder.localIdentifier] options:nil];
                if (fetchResult.count > 0) {
                    saveBlock(fetchResult.firstObject);
                }
            } else {
                NSLog(@"Error creating album: %@", error);
            }
        }];
    }
}

雨燕5

import Photos

func saveToAlbum(image: UIImage) {
    let albumName = "MyAlbum"

    let saveBlock: (PHAssetCollection) -> Void = { assetCollection in
        PHPhotoLibrary.shared().performChanges({
            let assetChangeRequest: PHAssetChangeRequest = PHAssetChangeRequest.creationRequestForAsset(from: image)
            guard let placeholder = assetChangeRequest.placeholderForCreatedAsset else { return }
            guard let assetCollectionChangeRequest: PHAssetCollectionChangeRequest = PHAssetCollectionChangeRequest(for: assetCollection) else { return }
            assetCollectionChangeRequest.addAssets(NSArray(object: placeholder))
        }) { (success, error) in
            if let error = error {
                print("Error creating asset: \(error)")
            }
        }
    }

    let fetchOptions = PHFetchOptions()
    fetchOptions.predicate = NSPredicate(format: "localizedTitle = %@", albumName)
    let fetchResult = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions)
    if fetchResult.count > 0, let collection = fetchResult.firstObject {
        saveBlock(collection)
    } else {
        var albumPlaceholder: PHObjectPlaceholder? = nil
        PHPhotoLibrary.shared().performChanges({
            let changeRequest = PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: albumName)
            albumPlaceholder = changeRequest.placeholderForCreatedAssetCollection
        }) { (success, error) in
            if success, let albumPlaceholder = albumPlaceholder {
                let fetchResult = PHAssetCollection.fetchAssetCollections(withLocalIdentifiers: [albumPlaceholder.localIdentifier], options: nil)
                if fetchResult.count > 0, let collection = fetchResult.firstObject {
                    saveBlock(collection)
                }
            } else if let error = error {
                print("Error creating album: \(error)")
            }
        }
    }
}

这篇关于将照片保存到iOS 8中的自定义相册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆