gpt4 book ai didi

swift - '获取选项中不支持谓词 : title == "Test"' in osx

转载 作者:行者123 更新时间:2023-12-04 17:57:14 25 4
gpt4 key购买 nike

我目前正在开发一个应用程序,我需要在该应用程序中显示每个相册中本地存储在手机上的图像。我设法显示了所有专辑名称,现在选择专辑时,我想显示图像。

func getLocalAlbums() {
var albums: [AlbumModel] = [AlbumModel]()
albumNamesDevice.removeAll()

let options = PHFetchOptions()
let topLvlOptions = PHFetchOptions()
let userAlbums = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .SmartAlbumUserLibrary, options: options)
let topLvlCollection = PHCollectionList.fetchTopLevelUserCollectionsWithOptions(topLvlOptions)

let allAlbums = [topLvlCollection, userAlbums]

for i in 0 ..< allAlbums.count {
let result = allAlbums[i]

result.enumerateObjectsUsingBlock { (asset, index, stop) in
if asset is PHAssetCollection {
let ass: PHAssetCollection = asset as! PHAssetCollection
let fetchOptions = PHFetchOptions()

fetchOptions.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.Image.rawValue)

let newAlbum = AlbumModel(name: ass.localizedTitle!, count: ass.estimatedAssetCount, collection: ass)
albums.append(newAlbum)
}
}
}

for item in albums {
self.albumNamesDevice.append(item.name)
}
}

func getAssetsFromAlbum(albumName: String) -> [PHAsset] {

let options = PHFetchOptions()
// Bug from Apple since 9.1, use workaround
options.predicate = NSPredicate(format: "title = %@", albumName)
options.sortDescriptors = [ NSSortDescriptor(key: "creationDate", ascending: true) ]

let results = PHAsset.fetchAssetsWithMediaType(.Image, options: options)
var assets = [PHAsset]()

results.enumerateObjectsUsingBlock { (obj, index, stop) in

if let asset = obj as? PHAsset {
assets.append(asset)
}
}

return assets
}

问题出在下面一行:

options.predicate = NSPredicate(format: "title = %@", albumName)

我不断收到错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unsupported predicate in fetch options: title == "Test"'

我读到这是 2014 年的一个错误,但苹果开发人员怎么还没有解决这个问题?

有人知道解决这个问题的方法吗?

最佳答案

最终我找到了答案,或者至少找到了解决方法

func getAssetsFromAlbum(albumName: String) -> [PHAsset] {

let options = PHFetchOptions()
// Bug from Apple since 9.1, use workaround
//options.predicate = NSPredicate(format: "title = %@", albumName)
options.sortDescriptors = [ NSSortDescriptor(key: "creationDate", ascending: true) ]

let collection: PHFetchResult = PHAssetCollection.fetchAssetCollectionsWithType(.Album, subtype: .Any, options: nil)

for k in 0 ..< collection.count {
let obj:AnyObject! = collection.objectAtIndex(k)
if obj.title == albumName {
if let assCollection = obj as? PHAssetCollection {
let results = PHAsset.fetchAssetsInAssetCollection(assCollection, options: options)
var assets = [PHAsset]()

results.enumerateObjectsUsingBlock { (obj, index, stop) in

if let asset = obj as? PHAsset {
assets.append(asset)
}
}

return assets
}
}
}
return [PHAsset]()
}

关于swift - '获取选项中不支持谓词 : title == "Test"' in osx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39385644/

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