【问题标题】:UIImagePickerControllerReferenceURL returns nil, crashing the appUIImagePickerControllerReferenceURL 返回 nil,导致应用程序崩溃
【发布时间】:2014-11-07 13:34:03
【问题描述】:

我正在尝试在 imagePickerController: didFinishPickingMediaWithInfo 函数中打印所有图像元数据。当我使用 info.objectForKey(UIImagePickerControllerReferenceURL) 方法时,它返回 nil,如果尝试使用此结果,我的应用程序将崩溃。有谁知道为什么它返回 nil 以及当我选择图像时我还能用什么来打印所有图像元数据? (使用 UIImageJPEGRepresentation 不是一个选项,因为 EXIF 数据已被删除)。 这是我的代码:

func imagePickerController(picker: UIImagePickerController!, didFinishPickingMediaWithInfo info:     NSDictionary!)
{
let image = info.objectForKey(UIImagePickerControllerOriginalImage) as UIImage

let refURL : NSURL = info.objectForKey(UIImagePickerControllerReferenceURL) as NSURL
var localSourceRef: CGImageSourceRef = CGImageSourceCreateWithURL(refURL, nil)
var localMetadata: NSDictionary = CGImageSourceCopyPropertiesAtIndex(localSourceRef, 0, nil)
println("\n Photo data: \n")
println(localMetadata) 
}

【问题讨论】:

  • UIImagePickerControllerReferenceURL 在几种情况下会为零,最常见的是来自相机(相对于相机胶卷),因为图像尚未保存。你能补充一些关于你到目前为止尝试过的测试用例的额外信息吗?
  • 没错,它适用于相机胶卷。但如果照片是用相机拍摄的,或者我尝试从照片库访问照片,它会崩溃。您对我如何从照片中获取 gps 数据有什么建议吗?谢谢!

标签: ios swift uiimagepickercontroller


【解决方案1】:

所以听起来这里实际上有两个问题:

1) 为什么 UIImagePickerControllerReferenceURL 返回一个 nil 引用?

2) 如何从照片中获取位置数据?

因此,(1)的答案通常是因为您在操作系统将文件写入图像库之前收到回调 didFinishPickingMedia。

#2 的答案要复杂得多,正如该问题的一系列答案所示: Reading the GPS data from the image returned by the camera in iOS iphone

您需要考虑许多变量:

  • 如果您没有请求访问位置数据,iOS 会删除 GPS 数据,因此您需要使用 CLLocationManager 提示您访问位置。
  • 如果用户禁用了地理标记,您将永远无法获得 GPS 坐标。
  • 如果手机无法获得 GPS 锁定,iOS 将不会记录 GPS 坐标。

根据这个答案:https://stackoverflow.com/a/10338012/490180 您应该能够检索原始 UIImage,然后从 UIImage 的 CGImage 的数据属性创建 CGImageSourceRef。这有效地消除了您访问 ReferenceURL 的需要。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2018-12-08
  • 1970-01-01
  • 1970-01-01
  • 2015-10-21
  • 2016-06-04
  • 2011-08-24
  • 2014-05-03
  • 1970-01-01
相关资源
最近更新 更多