4

我正在跟踪用户从哪个相册中选择了一张照片,并将其作为字符串 (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 所以没有收集/专辑被提取。

我错过了什么/搞砸了什么?

谢谢

4

2 回答 2

4

专辑名称不是本地标识符,这就是该方法fetchAssetCollectionsWithLocalIdentifiers返回的原因nil
此外,专辑的名称不是唯一的,并且可以创建多个具有相同名称的专辑,因此在这种情况下,您的应用可能无法正常运行。
我猜您之前已经获取了资产集合并将其保存localizedTitle在 string 中albumName
我建议您保留和使用localIdentifierassetcollection,而不是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
于 2016-03-22T14:53:02.587 回答
2

If you trying fetch collection by Album Name use code below

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

PHAssetCollection *collection = fetchResult.firstObject;

于 2015-12-29T13:50:59.940 回答