iOS Swift. How to get a GPS metadata from just a UIImage (NSURL)?

痞子三分冷 提交于 2019-12-04 13:50:14

问题


Have a reference to an image only having its NSURL. How to get a GPS metadata from it? Of course, I can load a UIImage from NSURL, but then what?

Majority of answers I've found here is regarding UIImagePicker, and using ALAssets then, but I have no such option.


回答1:


Answering my own question. The memory-effective and fast way to get a GPS metadata is

let options = [kCGImageSourceShouldCache as String: kCFBooleanFalse]
if let data = NSData(contentsOfURL: url), imgSrc = CGImageSourceCreateWithData(data, options) {
    let metadata = CGImageSourceCopyPropertiesAtIndex(imgSrc, 0, options) as Dictionary
    let gpsData = metadata[kCGImagePropertyGPSDictionary] as? [String : AnyObject]
}

The second option is

if let img = CIImage(contentsOfURL: url), metadata = img.properties(), 
gpsData = metadata[kCGImagePropertyGPSDictionary] as? [String : AnyObject] { … }

it looks nicer in Swift but uses more memory (tested via Profiler).




回答2:


Updated version for Swift 3:

let options = [kCGImageSourceShouldCache as String: kCFBooleanFalse]
if let data = NSData(contentsOfURL: url), let imgSrc = CGImageSourceCreateWithData(data, options as CFDictionary) {
    let metadata = CGImageSourceCopyPropertiesAtIndex(imgSrc, 0, options as CFDictionary) as? [String : AnyObject]
    if let gpsData = metadata?[kCGImagePropertyGPSDictionary as String] {
        //do interesting stuff here
    }
}


来源:https://stackoverflow.com/questions/29450629/ios-swift-how-to-get-a-gps-metadata-from-just-a-uiimage-nsurl

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