Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何用元数据丰富UIImage数据_Ios_Swift_Uiimage_Phasset - Fatal编程技术网

Ios 如何用元数据丰富UIImage数据

Ios 如何用元数据丰富UIImage数据,ios,swift,uiimage,phasset,Ios,Swift,Uiimage,Phasset,在我的应用程序中,我试图让图像数据包含元数据(位置、时间戳等)。我正在使用UIImagePickerController进行图像捕获,其委托函数具有: info[UIImagePickerController.InfoKey.originalImage] info[UIImagePickerController.InfoKey.phAsset] info[UIImagePickerController.InfoKey.mediaMetadata] 因此,对于从库中拾取的图像,phAsset具

在我的应用程序中,我试图让图像数据包含元数据(位置、时间戳等)。我正在使用UIImagePickerController进行图像捕获,其委托函数具有:

info[UIImagePickerController.InfoKey.originalImage]
info[UIImagePickerController.InfoKey.phAsset] 
info[UIImagePickerController.InfoKey.mediaMetadata]
因此,对于从库
中拾取的图像,phAsset
具有我所需的一切。我只是使用
.getDataFromPHAsset
函数来获取丰富的数据。但是,对于刚刚拍摄的图像,
。phAsset
为零。我曾考虑尝试以某种方式将
.originalImage
.mediaMetadata
组合到一个数据对象中,但无法获得所需的结果。我尝试使用这种方法:

我知道我也可以使用
AVCaptureSession
定制imageCapture控制器,并在
didFinishProcessingPhoto
委托调用中使用
AVCapturePhoto
函数
.fileDataRepresentation()
,但这不是我的第一选择


非常感谢您提供的任何帮助。

我确信mediaMetadata不会提供位置信息。用这个

然后使用下面的函数将元数据添加到图像数据中:

func addImageProperties(imageData: Data, properties: NSMutableDictionary) -> Data? {
  let dict = NSMutableDictionary()
  dict[(kCGImagePropertyGPSDictionary as String)] = properties

  if let source = CGImageSourceCreateWithData(imageData as CFData, nil) {
    if let uti = CGImageSourceGetType(source) {
      let destinationData = NSMutableData()
      if let destination = CGImageDestinationCreateWithData(destinationData, uti, 1, nil) {
        CGImageDestinationAddImageFromSource(destination, source, 0, dict as CFDictionary)
        if CGImageDestinationFinalize(destination) == false {
          return nil
        }                    
        return destinationData as Data
      }
    }
  }
  return nil
}
用法:

if let imageData = image.jpegData(compressionQuality: 1.0), let metadata = locationManager.location?.exifMetadata() {
  if let newImageData = addImageProperties(imageData: imageData, properties: metadata) {
    // newImageData now contains exif metadata
  }
}