PhotosFramework 将资产分配给相册
Posted
技术标签:
【中文标题】PhotosFramework 将资产分配给相册【英文标题】:PhotosFramework assign asset to album 【发布时间】:2016-08-07 02:24:20 【问题描述】:我查看了几个示例,但无法弄清楚这一点。我觉得我在这里遗漏了一些简单的东西。
在我的应用程序中,我想在特定相册 (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 照片应用程序可以将图像分配给多个相册,并且不会创建多个副本。
【问题讨论】:
【参考方案1】:您正在将新图像添加到库中,因为您明确请求将新图像添加到库中:
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
块 — 您可以创建相册并将其添加到同一个更改请求中。
【讨论】:
完美,谢谢!!这完全符合我的需要。使用相同的执行更改请求的额外好处。以上是关于PhotosFramework 将资产分配给相册的主要内容,如果未能解决你的问题,请参考以下文章
如何在我的 collectionView 中获取 Moment 相册和所有个人相册?
如何从资产中获取不同的图像并将其分配给不同表格视图单元格中的图像视图