Retrieve ALAsset or PHAsset from file URL

十年热恋 提交于 2019-11-27 06:50:52

问题


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?

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.


回答1:


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.

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
}



回答2:


So this is commentary on ("refinement tips") to your auto-answer. SO comments don't cut it for code samples, so here we go.

  1. You can replace your for-index loop with a simpler for-each loop. E.g. something like:

    for asset in PHAsset.fetchAssetsWithOptions(nil)
    
  2. As of the last time I checked, the key in info["PHImageFileURLKey"] is undocumented. Be apprised. I don't think it will get you rejected, but the behavior could change at any time.



来源:https://stackoverflow.com/questions/28661938/retrieve-alasset-or-phasset-from-file-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!