gpt4 book ai didi

ios - 错误 : 'Unsupported predicate in fetch options: mediaType == 2'

转载 作者:技术小花猫 更新时间:2023-10-29 10:18:23 27 4
gpt4 key购买 nike

我正在尝试使用 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 内的第一行不断中断并给出以下错误:由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“获取选项中不支持谓词:mediaType == 2”

我做了一些调查,发现了这个 link .
我想知道这是 Apple 的错误还是我的代码有问题。
它似乎对提到这个 answer 的人有用,这太奇怪了,因为它基本上是同一件事。


提前致谢,
安尼施

编辑:
我想我找到了答案 here在 Apple 的文档中。看起来 mediaType 只是 PHAsset 而不是 PHAssetCollection 的键。所以现在我想问题是如何获取仅包含视频或图片的 PFAssetCollection

最佳答案

使用该方法分别获取照片和视频资源数组。

获取所有视频

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)
}

关于ios - 错误 : 'Unsupported predicate in fetch options: mediaType == 2' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35590640/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com