我看过几个例子,却找不出这个例子。我觉得我错过了一些简单的东西。
在我的应用程序中,我想在一个特定的相册(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应用程序,您可以分配一个图像到多个相册,它不会创建多个副本。
发布于 2016-08-08 17:40:48
将一个新映像添加到库中是因为您明确请求将一个新图像添加到库中:
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块-您可以创建相册并在相同的更改请求中添加到其中。
https://stackoverflow.com/questions/38810326
复制相似问题