使用位掩码的NSPredicate过滤NSArray

4

我正在尝试使用一个NSPredicate测试特定资产集是否仅包含一个媒体类型/子类型。 testForPhotosPredicate正常工作,但是当尝试使用testForPanoramasPredicate时,出现错误消息:无法解析格式字符串"mediaSubtypes & %i"

如何在此谓词中使用位掩码以进行媒体子类型?

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");
                }
            }
        }
    }
}
1个回答

4
我相信我已经用以下代码解决了这个问题:
NSPredicate *testForPanoramasPredicate = [NSPredicate predicateWithFormat:@"(mediaSubtypes & %i) == %i",PHAssetMediaSubtypePhotoPanorama,PHAssetMediaSubtypePhotoPanorama];

NSArray *testForAllPanoramas = [testForAllPhotos filteredArrayUsingPredicate:testForPanoramasPredicate];

也许更好的方法是,如果您不想进行初始图像谓词:
NSPredicate *testForPanoramasPredicate = [NSPredicate predicateWithFormat:
@"(mediaType == %i && (mediaSubtypes & %i) == %i))",
PHAssetMediaTypeImage,PHAssetMediaSubtypePhotoPanorama,PHAssetMediaSubtypePhotoPanorama];

以下是完整代码,适用于想要检查相册是否包含特定媒体子类型的用户;这对于在相册上显示UI徽章非常有用。

PHFetchResult *assetsInCollection = [PHAsset fetchAssetsInAssetCollection:sub options:fetchOptions];

if (assetsInCollection.count > 0)
{
    NSArray *allAssets = [assetsInCollection objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, assetsInCollection.count)]];

    NSPredicate *testForPanoramasPredicate = [NSPredicate predicateWithFormat:@"(mediaType == %i && (mediaSubtypes & %i) == %i))",PHAssetMediaTypeImage,PHAssetMediaSubtypePhotoPanorama,PHAssetMediaSubtypePhotoPanorama];

    NSPredicate *testForHDRPredicate = [NSPredicate predicateWithFormat:@"(mediaType == %i && (mediaSubtypes & %i) == %i))",PHAssetMediaTypeImage,PHAssetMediaSubtypePhotoHDR,PHAssetMediaSubtypePhotoHDR];

    NSPredicate *testForVideosPredicate = [NSPredicate predicateWithFormat:@"mediaType = %i",PHAssetMediaTypeVideo];

    NSPredicate *testForSlomoPredicate = [NSPredicate predicateWithFormat:@"(mediaType == %i && (mediaSubtypes & %i) == %i))",PHAssetMediaTypeVideo,PHAssetMediaSubtypeVideoHighFrameRate,PHAssetMediaSubtypeVideoHighFrameRate];

    NSPredicate *testForTimelapsePredicate = [NSPredicate predicateWithFormat:@"(mediaType == %i && (mediaSubtypes & %i) == %i))",PHAssetMediaTypeVideo,PHAssetMediaSubtypeVideoTimelapse,PHAssetMediaSubtypeVideoTimelapse];

    NSPredicate *testForStreamedPredicate = [NSPredicate predicateWithFormat:@"(mediaType == %i && (mediaSubtypes & %i) == %i))",PHAssetMediaTypeVideo,PHAssetMediaSubtypeVideoStreamed,PHAssetMediaSubtypeVideoStreamed];

    NSArray *testForAllPanoramas = [allAssets filteredArrayUsingPredicate:testForPanoramasPredicate];
    NSArray *testForAllHDR = [allAssets filteredArrayUsingPredicate:testForHDRPredicate];
    NSArray *testForAllVideos = [allAssets filteredArrayUsingPredicate:testForVideosPredicate];
    NSArray *testForAllSlomo = [allAssets filteredArrayUsingPredicate:testForSlomoPredicate];
    NSArray *testForAllTimelapse = [allAssets filteredArrayUsingPredicate:testForTimelapsePredicate];
    NSArray *testForAllStreamed = [allAssets filteredArrayUsingPredicate:testForStreamedPredicate];

    NSArray *allPossibilitiesArray = @[testForAllPanoramas,testForAllHDR,testForAllVideos,testForAllSlomo,testForAllTimelapse,testForAllStreamed];

    for (NSArray *sub in allPossibilitiesArray)
    {
        if (sub.count == allAssets.count)
        {
            PHAsset *firstAsset = sub.firstObject;

            [dataSource setObject:[NSNumber numberWithInt:firstAsset.mediaSubtypes] forKey:@"MediaSubtype"];
        }
    }
}

2
你可能想在 (mediaSubtypes & %i) 周围加上 (),因为 & 的优先级低于 ==,所以它最终会评估 (mediaSubtypes & (%i == %i)) - dan
@dan 很好的发现!谢谢你。 - klcjr89

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接