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

编程入门 行业动态 更新时间:2024-10-23 08:41:12
本文介绍了错误:“提取选项中不支持的谓词: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还是我的代码有问题. 似乎对于引用此 answer ,这很奇怪,因为它基本上是同一回事.

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"

本文发布于:2023-10-29 10:09:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1539420.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:谓词   不支持   错误   选项中   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!