iOS Swift。如何从UIImage(NSURL)获取GPS元数据?

时间:2015-04-04 19:34:34

标签: ios swift gps uiimage nsurl

引用仅具有NSURL的图像。 如何从中获取GPS元数据? 当然,我可以从NSURL加载UIImage,但接下来是什么?

我在这里找到的大部分答案都是关于UIImagePicker,然后使用ALAssets,但我没有这样的选择。

2 个答案:

答案 0 :(得分:6)

回答我自己的问题。获取GPS元数据的记忆效率和快速方法是

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]
}

第二个选项是

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

它在Swift中看起来更好但使用更多内存(通过Profiler测试)。

答案 1 :(得分:0)

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
    }
}