PhotosFramework 将资产分配给相册

PhotosFramework assign asset to album

我看过几个示例,但无法弄清楚这个。我觉得我在这里遗漏了一些简单的东西。

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

如果我保存图像的另一个副本,我就可以成功地将图像分配到相册 XYZ。也就是说,如果我现在返回相机胶卷,我将看到同一图像的两个副本。一个分配给专辑 ABC,另一个分配给 XYZ。这不是我想要的。我有数以千计的图像将分配到相册 XYZ,我不希望两个副本不必要地占用 space。

这是我将图像副本保存到新相册的代码:

-(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 照片应用程序可以将一张图片分配给多个相册,它不会创建多个副本。

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

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 块 — 您可以创建相册并在同一更改请求中添加。