错误:'提取选项中不支持的谓词:mediaType == 2'

时间:2016-02-23 23:52:26

标签: ios iphone nspredicate photosframework

我尝试使用smartAlbum生成一组只有视频或只有照片或两者兼而有之。 您可以在下面看到我的代码:

    PHFetchResult *collectionList = [PHCollectionList fetchMomentListsWithSubtype:PHCollectionListSubtypeMomentListCluster options:nil];
    PHFetchOptions *options = nil;
        if (self.xSelected) {
            options = [[PHFetchOptions alloc] init];
            options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
            options.predicate = [NSPredicate predicateWithFormat:@"mediaType = %d",PHAssetMediaTypeImage];
        }
        if (self.ySelected) {
            options = [[PHFetchOptions alloc] init];
            options.predicate = [NSPredicate predicateWithFormat:@"mediaType = %d",PHAssetMediaTypeVideo];
        }

    [collectionList enumerateObjectsUsingBlock:^(PHCollectionList *collection, NSUInteger idx, BOOL *stop) {
        PHFetchResult *momentsInCollection = [PHAssetCollection fetchMomentsInMomentList:collection options:options];
        for (id moment in momentsInCollection) {
            PHAssetCollection *castedMoment = (PHAssetCollection *)moment;
            [_smartAlbums insertObject:castedMoment atIndex:0];
        }
    }];


然而,这会在block内的第一行不断中断并出现以下错误: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unsupported predicate in fetch options: mediaType == 2'

我做了一点研究,发现了link 我想知道这是Apple的错误还是我的代码错了。
它似乎对那些提到这个answer的人有用,这是如此奇怪,因为它基本上是相同的。


提前谢谢,
阿尼什

修改:
我想我在Apple的文档中找到了答案here。看起来mediaType只是PHAsset而非PHAssetCollection的关键。所以现在我想问题是如何只用视频或图像来获取PFAssetCollection

2 个答案:

答案 0 :(得分:0)

使用此方法分别获取照片和视频资产阵列。

获取所有视频

func getAllVideos(completion:@escaping  (_ videoAssets : [PHAsset]?) -> Void) {
    var videoAssets : [PHAsset] = []

    let fetchOptions = PHFetchOptions()
    fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate",ascending: false)]
    fetchOptions.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.video.rawValue)
    let allVideo = PHAsset.fetchAssets(with: .video, options: fetchOptions)
    allVideo.enumerateObjects { (asset, index, bool) in
        videoAssets.append(asset)
    }
    completion(videoAssets)
}

获取所有照片

func getAllPhotos(completion:@escaping  (_ photosAssets : [PHAsset]?) -> Void) {
    var photosAssets : [PHAsset] = []

    let fetchOptions = PHFetchOptions()
    let scale = UIScreen.main.scale
    let screenWidth = UIScreen.main.bounds.width * scale
    let screenHeight = UIScreen.main.bounds.height  * scale

    fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate",ascending: false)]
    fetchOptions.predicate = NSPredicate(format: "mediaType = %d || (mediaSubtype & %d) != 0 && (pixelHeight != %d AND pixelWidth != %d) OR (pixelHeight != %d AND pixelWidth != %d)", PHAssetMediaType.image.rawValue, PHAssetMediaSubtype.photoLive.rawValue ,screenHeight, screenWidth, screenWidth, screenHeight)

    let allPhotos = PHAsset.fetchAssets(with: .image, options: fetchOptions)
    allPhotos.enumerateObjects { (asset, index, bool) in
        photosAssets.append(asset)
    }
    completion(photosAssets)
}

答案 1 :(得分:0)

无需在谓词中放入 mediaType swift 4.0 中,这就是我使用照片框架中的fetchAsset()方法从照片库中获取所有视频的方法。

您还可以使用谓词从特定文件夹中获取视频。

   func fetchAllVideos()
{
    //let albumName = "blah"
    let fetchOptions = PHFetchOptions()
    //        fetchOptions.predicate = NSPredicate(format: "title = %@", albumName)
    //uncomment this if you want video from custom folder
    fetchOptions.predicate = NSPredicate(format: "mediaType = %d ", PHAssetMediaType.video.rawValue )

    let allVideo = PHAsset.fetchAssets(with: .video, options: fetchOptions)
    allVideo.enumerateObjects { (asset, index, bool) in
        // videoAssets.append(asset)
        let imageManager = PHCachingImageManager()
        imageManager.requestAVAsset(forVideo: asset, options: nil, resultHandler: { (asset, audioMix, info) in
            if asset != nil {
                let avasset = asset as! AVURLAsset
                let urlVideo = avasset.url
                print(urlVideo)

            }
        })

    }

}

希望得到帮助!