I'm trying to test whether some particular asset collections contain only one media type/subtype using an NSPredicate
. The: testForPhotosPredicate
works just fine, however when trying to use the testForPanoramasPredicate
it fails with the message: Unable to parse the format string "mediaSubtypes & %i"
How can I use a bitmask in this predicate for the mediaSubtypes?
for (PHFetchResult *newFetch in collectionFetches)
{
for (PHAssetCollection *sub in newFetch)
{
PHFetchResult *assetsInCollection = [PHAsset fetchAssetsInAssetCollection:sub options:fetchOptions];
NSArray *allAssets = [assetsInCollection objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, assetsInCollection.count)]];
if (allAssets.count > 0)
{
[allAssetsArray addObjectsFromArray:allAssets];
NSPredicate *testForPhotosPredicate = [NSPredicate predicateWithFormat:@"mediaType = %i",PHAssetMediaTypeImage];
NSArray *testForAllPhotos = [allAssets filteredArrayUsingPredicate:testForPhotosPredicate];
if (testForAllPhotos.count == allAssets.count)
{
NSPredicate *testForPanoramasPredicate = [NSPredicate predicateWithFormat:@"mediaSubtypes & %i",PHAssetMediaSubtypePhotoPanorama];
NSArray *testForAllPanoramas = [testForAllPhotos filteredArrayUsingPredicate:testForPanoramasPredicate];
if (testForAllPanoramas.count == testForAllPhotos.count)
{
NSLog(@"all panos");
}
}
}
}
}
I believe I have solved it with the below code:
Perhaps even better yet if you don't want to do the initial image predicate:
Full code to help others out who specifically want to check if an album contains one particular media subtype; useful for displaying a UI badge on an album.