当我启动一个图片抓取功能时:
let assetCollections: PHFetchResult = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .Any, options: nil) 我得到了几张专辑的结果,所以我可以遍历数组并将它们的标题放在一个tableView中。
但是:当我使用谓词过滤并获取相册"Camera Roll“时,结果总是一个空数组:(我100%确定Camera Roll存在,因为没有任何选项,它会获取它)
let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSPredicate(format: "title = %@", "Camera Roll")
let assetCollections: PHFetchResult = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .Any, options: fetchOptions)
let album: PHAssetCollection = assetCollections.firstObject as! PHAssetCollection我在网上读过4到5种不同的方法,他们都有这个谓词,无论是"title = %@",还是"localizedTitle = %@“或"localizedIdentifier =%@”……我还是不明白。似乎是为人们工作,而不是为我。编译器在最后一行尝试“展开一个nil可选值”(‘因为读取结果为空)时崩溃。为什么只要包含读取选项,搜索就会返回nil?
发布于 2015-12-04 09:31:49
如果你想要Camera Roll相册,你最好象征性地索要它:
let collections = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .SmartAlbumUserLibrary, options: nil)SmartAlbumUserLibrary子类型是获取摄影机滚动的方式(并不总是这样叫的)。
发布于 2015-11-17 22:53:56
解决方案似乎是使用:
PHAssetCollection.fetchAssetCollectionsWithLocalizedIdentifiesr(identifiers: [String], options: PHFetchOptions)因此,将带有我们希望获取的专辑标题的字符串数组传递到参数'identifier‘中,可以得到与尝试似乎不再有效的谓词方法相同的结果。
发布于 2017-10-10 23:03:06
如果你只想要Camera Roll相册,你可以尝试这样的东西:
func getCameraRoll() -> AlbumModel {
var cameraRollAlbum : AlbumModel!
let cameraRoll = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .smartAlbumUserLibrary, options: nil)
cameraRoll.enumerateObjects({ (object: AnyObject!, count: Int, stop: UnsafeMutablePointer) in
if object is PHAssetCollection {
let obj:PHAssetCollection = object as! PHAssetCollection
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
fetchOptions.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.image.rawValue)
let assets = PHAsset.fetchAssets(in: obj, options: fetchOptions)
if assets.count > 0 {
let newAlbum = AlbumModel(name: obj.localizedTitle!, count: assets.count, collection:obj, assets: assets)
cameraRollAlbum = newAlbum
}
}
})
return cameraRollAlbum
}请注意,您应该AlbumModel是我创建的模型,您可以更改它并使用您自己的数据模型。
https://stackoverflow.com/questions/33681165
复制相似问题