本文介绍了iOS Swift。如何从UIImage(NSURL)获取GPS元数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

回答我自己的问题。获取GPS元数据的内存有效和快速的方式是

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

第二个选项是

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

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


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.

解决方案

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).

这篇关于iOS Swift。如何从UIImage(NSURL)获取GPS元数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 13:49