简体   繁体   English

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

[英]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.

相关问题 如何快速从 UIImagePickerController 获取 UIImage 元数据? 像“.location” - How to get UIImage metadata from UIImagePickerController in swift ? like ".location" 我在Swift中创建了一个UIImage作为视频的快照。 我如何获得它的临时路径? - I created a UIImage as a snapshot of my video in Swift. How can I get a temporary path to it? iOS和Swift - 如何从用户那里获取和保存UIImage? - iOS & Swift - How can I get and save a UIImage from the user? 迅速。 如何从扩展名获取泛型? - Swift. How to get generic type from extension? iOS Swift-Instagram API从WebView NSURL请求获取响应 - iOS Swift - Instagram API get response from WebView NSURL Request 如何使用 Swift 通过 PHAsset 访问 UIImage 的元数据 - How to get access to metadata for UIImage via PHAsset using Swift 如何从 AnyPublisher 获取 UIImage<UIImage?, Never> Swift 5(组合框架) - How to get UIImage from AnyPublisher<UIImage?, Never> Swift 5 (Combine framework) 如何从Swift beta 3中的UIImagePickerControllerDelegate获取UIImage? - How to get UIImage from UIImagePickerControllerDelegate in Swift beta 3? 如何快速从文档目录中的特定文件获取NSURL - How to get NSURL on specific file from document directory in swift Swift。 如何从闭包中返回 function - Swift. How to return a function from a closure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM