错误:“提取选项中不支持的谓词:mediaType == 2" [英] Error: 'Unsupported predicate in fetch options: mediaType == 2'

查看:298
本文介绍了错误:“提取选项中不支持的谓词:mediaType == 2"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用smartAlbum生成仅视频或仅照片或两者的数组.
您可以在下面看到我的代码:

I'm trying to use smartAlbum to generate an array of either only videos or only photos or both.
You can see my code below:

    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'


This however is constantly breaking on the first line inside the block and giving the following error: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unsupported predicate in fetch options: mediaType == 2'

我做了一些研究,发现了这个链接.
我想知道这是Apple bug还是我的代码有问题.
似乎对于引用此

I did a little research and found this link.
I'm wondering if this is an Apple bug or if its just something wrong with my code.
It seems like it worked for the people that referred to this answer, which is so weird coz its basically the same thing.


预先感谢,
阿尼什(Anish)


Thanks in advance,
Anish


我想我找到了答案
此处.看起来mediaType仅是PHAsset的键,而不是PHAssetCollection的键.因此,现在我想问题是如何仅使用视频或图像来获取PFAssetCollection.


I think I found the answer here in Apple's Documentation. Looks like mediaType is a key only for PHAsset and not PHAssetCollection. So now I guess the question is how to get PFAssetCollection with only videos or only images.

推荐答案

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

No need to put mediaType in predicate In swift 4.0 this is how i used fetchAsset() method from Photos framework , to get all videos from photo library.

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

You can also get the video from specific folder using predicate.

   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)

            }
        })

    }

}

希望得到帮助!

这篇关于错误:“提取选项中不支持的谓词:mediaType == 2"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆