[英]iOS Swift. How to get a GPS metadata from just a UIImage (NSURL)?
Have a reference to an image only having its NSURL. 引用仅具有NSURL的图像。 How to get a GPS metadata from it?
如何从中获取GPS元数据? Of course, I can load a UIImage from NSURL, but then what?
当然,我可以从NSURL加载UIImage,但那又怎么样?
Majority of answers I've found here is regarding UIImagePicker, and using ALAssets then, but I have no such option. 我在这里找到的大多数答案都是关于UIImagePicker,然后使用ALAssets,但我没有这样的选择。
Answering my own question. 回答我自己的问题。 The memory-effective and fast way to get a GPS metadata is
获取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]
}
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). 它在Swift中看起来更好但使用更多内存(通过Profiler测试)。
Updated version for Swift 3: 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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.