ios - 带位掩码的 NSPredicate 用于过滤 NSArray

标签 ios objective-c nsarray nspredicate photokit

我正在尝试使用 NSPredicate 测试某些特定 Assets 集合是否仅包含一种媒体类型/子类型。 testForPhotosPredicate 工作得很好,但是当尝试使用 testForPanoramasPredicate 时失败并显示消息:无法解析格式字符串“mediaSubtypes & %i”

如何在此谓词中为 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");
                }
            }
        }
    }
}

最佳答案

我相信我已经用下面的代码解决了这个问题:

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 角标(Badge)。

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"];
        }
    }
}

关于ios - 带位掩码的 NSPredicate 用于过滤 NSArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30789546/

相关文章:

xcode - 将 NSArray 转换为 NSString 会崩溃?

asp.net - 无法从 JSON Webservice 获取值

ios - 无法使用 Box iOS SDK 2.0 存档项目

充当信标的 Android 设备

ios - 如何使用 NSDateFormatter 将字符串日期转换为 NSDate

ios - 使用多人连接发送和接收邀请

objective-c - 为什么@YES 给出 "expected expression"错误,但 @(YES) 编译?

c# - 是否可以从网络应用程序访问手机的联系人?

iphone - 为什么下面的代码行没有向数组添加任何内容,而数组只产生 null

iOS - 将 NSDictionaries 嵌套到 NSArray