2015-02-09 70 views
3

我正在跟踪用户选择照片并将其作为字符串(albumName)传递给下一个VC的相册。使用照片框架使用本地标识符获取相册

我想只提取该相册中的照片以供进一步选择和处理。

这就是我想会做的伎俩,但我必须失去了一些东西:

-(void) fetchImages{ 
    self.assets = [[PHFetchResult alloc]init]; 
     NSLog(@"Album Name:%@",self.albumName); 

    if (self.fromAlbum) { 


     PHFetchResult *userAlbums = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[self.albumName] options:nil]; 
     PHAssetCollection *collection = userAlbums[0]; 

     PHFetchOptions *onlyImagesOptions = [PHFetchOptions new]; 
     onlyImagesOptions.predicate = [NSPredicate predicateWithFormat:@"mediaType = %i", PHAssetMediaTypeImage]; 

     NSLog(@"Collection:%@", collection.localIdentifier); 

     self.assets = [PHAsset fetchAssetsInAssetCollection:collection options:onlyImagesOptions]; 

..... 

当我登录的collection.localIdentifier我得到null所以没有收集/专辑是牵强。

我在想什么/搞砸了?

感谢

回答

2

如果你想通过专辑名称取收集使用下面

PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init]; 
    fetchOptions.predicate = [NSPredicate predicateWithFormat:@"title = %@", albumNamed]; 
    PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum 
                  subtype:PHAssetCollectionSubtypeAny 
                  options:fetchOptions]; 

PHAssetCollection *集合= fetchResult.firstObject代码;

1

相册名称不是本地标识符,这就是为什么方法fetchAssetCollectionsWithLocalIdentifiers返回nil
专辑的名称也不是唯一的,可以使用相同的名称创建多个专辑,因此在这种情况下,您的应用可能无法正常工作。
我想你以前已经提取了资产集合,并保留其'localizedTitle字符串albumName
我建议你保留并使用localIdentifier的assetscollection而不是localizedTitle并将其传递给VC。然后,您将可以使用该标识符轻松获取资产。

//Assume we have previously done this to fetch album name and identifier 
PHFetchResult * myFirstFetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:nil]; 
PHAssetCollection * myFirstAssetCollection = myFirstFetchResult.firstObject; 
NSString * albumName = myFirstAssetCollection.localizedTitle; 
NSString * albumIdentifier = myFirstAssetCollection.localIdentifier; //<-Add this... 

//Pass albumIdentifier to VC... 

//Inside your 'fetchImages' method use this to get assetcollection from passed albumIdentifier 
PHFetchResult *userAlbums = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[self.albumIdentifier] options:nil]; 
PHAssetCollection *collection = userAlbums.firstObject; 
//Now you have successfully passed and got asset collection and you can use 
相关问题