簡體   English   中英

NSPredicate使用位掩碼過濾NSArray

[英]NSPredicate with bitmask for filtering NSArray

我正在嘗試使用NSPredicate來測試某些特定資產集合是否只包含一種媒體類型/子類型。 testForPhotosPredicate工作正常,但是當嘗試使用testForPanoramasPredicate它失敗並顯示以下消息: Unable to parse the format string "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徽章。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM