只有NSURL引用的图像如何获取GPS元数据?
当然,我可以从NSURL加载UIImage,但接下来呢?
我在这里找到的大多数答案都涉及UIImagePicker和使用ALAssets,但我没有这样的选项。
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测试)。
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
}
}