I am keeping track from which album the user has selected a photo and passing it as a string (albumName) to the next VC.
I want to fetch only the photos in that album for further selection and processing.
this is what I thought would do the trick but I must be missing something:
-(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];
.....
when i log the collection.localIdentifier
i get null
so no collection/album is fetched.
What am I missing/messing up?
Thanks
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;
Album name is not a local identifier that's why the method fetchAssetCollectionsWithLocalIdentifiers
returns nil
.
Also names of albums are not unique, and it's possible to create multiple albums with the same name, so in this case your app may not work properly.
I guess that you have previously fetched asset collections and kept its' localizedTitle
in string albumName
.
I would recommend you to keep and use localIdentifier
of assetcollection instead of localizedTitle
and pass it to VC. Then you'll be able to easily fetch assets by using that identifier.
//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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.