从文件URL中检索ALAsset或PHAsset [英] Retrieve ALAsset or PHAsset from file URL
问题描述
选择Photos.app中的图像传递给动作扩展名似乎会产生磁盘上图像的路径(例如: file:///var/mobile/Media/DCIM/109APPLE/IMG_9417.JPG
)。有没有办法获得相应的ALAsset或PHAsset?
Selecting images in Photos.app to pass to an action extension seems to yield paths to images on disk (e.g.: file:///var/mobile/Media/DCIM/109APPLE/IMG_9417.JPG
). Is there a way to get the corresponding ALAsset or PHAsset?
URL看起来与调用 PHImageManager得到的
。我不想迭代所有的PHAssets来找到它。 PHImageFileURLKey
条目相对应。 requestImageDataForAsset
The URL looks like it corresponds to the PHImageFileURLKey
entry you get from calling PHImageManager.requestImageDataForAsset
. I'd hate to have to iterate through all PHAssets to find it.
推荐答案
我做了我不想做的事情并将这种愚蠢的搜索方法放在一起。虽然它很糟糕,但很慢并且当照片库很大时会给我带来记忆问题。
I did what I didn't want to do and threw this dumb search approach together. It works, although it's horrible, slow and gives me memory issues when the photo library is large.
作为Cocoa和Swift的菜鸟,我很欣赏细化技巧。谢谢!
As a noob to both Cocoa and Swift I'd appreciate refinement tips. Thanks!
func PHAssetForFileURL(url: NSURL) -> PHAsset? {
var imageRequestOptions = PHImageRequestOptions()
imageRequestOptions.version = .Current
imageRequestOptions.deliveryMode = .FastFormat
imageRequestOptions.resizeMode = .Fast
imageRequestOptions.synchronous = true
let fetchResult = PHAsset.fetchAssetsWithOptions(nil)
for var index = 0; index < fetchResult.count; index++ {
if let asset = fetchResult[index] as? PHAsset {
var found = false
PHImageManager.defaultManager().requestImageDataForAsset(asset,
options: imageRequestOptions) { (_, _, _, info) in
if let urlkey = info["PHImageFileURLKey"] as? NSURL {
if urlkey.absoluteString! == url.absoluteString! {
found = true
}
}
}
if (found) {
return asset
}
}
}
return nil
}
这篇关于从文件URL中检索ALAsset或PHAsset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!