PhotosFramework将资产分配给相册

时间:2016-08-07 02:24:21

标签: objective-c photosframework

我看过几个例子,但是无法想出这个例子。我觉得我在这里缺少一些简单的东西。

在我的应用中,我想拍摄特定相册(ABC)中的图像并将其添加到我专门为此应用创建的另一个相册(XYZ)中。从IOS的默认照片应用程序中可以看到这两张专辑。

如果我保存图像的另一个副本,我可以成功地将图像分配到相册XYZ。即如果我现在回到相机胶卷,我会看到同一张图像的两个副本。一个分配给专辑ABC,另一个分配给XYZ。这不是我想要的。我有数以千计的图像将被分配到专辑XYZ,我不希望两个副本不经意地占用空间。

以下是我将代码副本保存到新相册的代码:

-(void) saveImage:(UIImage*) imageToSave toCollection:(NSString *) collectionTitle forRowNumber:(long) rowNumber{

    NSLog(@"entered %s", __PRETTY_FUNCTION__);
    __block PHFetchResult *photosAsset;
    __block PHAssetCollection *collection;
    __block PHObjectPlaceholder *placeholder;

        // Find the album
    PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];

    fetchOptions.predicate = [NSPredicate predicateWithFormat:@"title = %@", collectionTitle];
                // this is how we get a match for album Title held by 'collectionTitle'

    collection = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:fetchOptions].firstObject;

        // check if album exists
    if (!collection)
    {
        [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{

            NSLog(@" Album did not exist, now creating album: %@",collectionTitle);
                // Create the album
            PHAssetCollectionChangeRequest *createAlbum = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:collectionTitle];

            placeholder = [createAlbum placeholderForCreatedAssetCollection];

        } completionHandler:^(BOOL didItSucceed, NSError *error) {
            if (didItSucceed)
            {
                PHFetchResult *collectionFetchResult = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[placeholder.localIdentifier] options:nil];

                collection = collectionFetchResult.firstObject;
            }
        }];
    }

        // Save to the album
    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{

        PHAssetChangeRequest *assetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:imageToSave];

        placeholder = [assetRequest placeholderForCreatedAsset];
        photosAsset = [PHAsset fetchAssetsInAssetCollection:collection options:nil];

        PHAssetCollectionChangeRequest *albumChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:collection assets:photosAsset];

        [albumChangeRequest addAssets:@[placeholder]];

    } completionHandler:^(BOOL didItSucceed, NSError *error) {

        if (didItSucceed)
        {       // if YES


            NSLog(@" Looks like Image was saved in camera Roll as %@", placeholder.localIdentifier);
            NSLog(@"placeholder holds %@", placeholder.debugDescription );

        }
        else
        {
            NSLog(@"%@", error);
        }

    }];


}

我知道使用IOS Photos应用程序可以将图像分配给多个相册,但不会创建多个副本。

1 个答案:

答案 0 :(得分:4)

您正在将新图像添加到库中,因为您明确要求将新图像添加到库中:

PHAssetChangeRequest *assetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:imageToSave];

如果您有PHAsset代表原始图片,则可以请求将同一资源添加到您的第二张专辑中。但由于您共享的代码是从UIImage开始的,因此您已失去与原始资产的连接。

假设你某处有PHAsset,你需要的东西看起来像这样:

- (void)saveAsset:(PHAsset *)asset toCollectionNamed:(NSString *) collectionTitle {

    // Find the album
    PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
    fetchOptions.predicate = [NSPredicate predicateWithFormat:@"title = %@", collectionTitle];
    PHAssetCollection *collection = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:fetchOptions].firstObject;

    // Combine (possible) album creation and adding in one change block
    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
        PHAssetCollectionChangeRequest *albumRequest;
        if (collection == nil) {
            // create the album if it doesn't exist
            albumRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:collectionTitle];
        } else {
            // otherwise request to change the existing album
            albumRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:collection];
        }
        // add the asset to the album
        [albumRequest addAssets:@[asset]];

    } completionHandler:^(BOOL success, NSError *error) {
        if (!success) {
            NSLog(@"error creating or adding to album: %@", error);
        }
    }];
}

请注意,您不需要两个PHPhotoLibrary performChanges块 - 您可以创建相册并在同一更改请求中添加相册。