【问题标题】:How to enrich UIImage data with metadata如何使用元数据丰富 UIImage 数据
【发布时间】:2019-05-03 00:28:26
【问题描述】:

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

info[UIImagePickerController.InfoKey.originalImage]
info[UIImagePickerController.InfoKey.phAsset] 
info[UIImagePickerController.InfoKey.mediaMetadata]

所以对于从库中挑选的图像.phAsset 有我需要的一切。我只是使用.getDataFromPHAsset 函数来获取丰富的数据。但是,对于刚刚拍摄的图像,.phAsset 为零。我想过尝试以某种方式将 .originalImage.mediaMetadata 组合成单个 Data 对象,但无法获得所需的结果。我尝试使用这种方法:https://gist.github.com/kwylez/a4b6ec261e52970e1fa5dd4ccfe8898f

我知道我也可以使用 AVCaptureSession 制作自定义 imageCapture 控制器,并在 didFinishProcessingPhoto 委托调用上使用 AVCapturePhoto 函数 .fileDataRepresentation(),但这不是我的首选。

非常感谢任何形式的帮助。

【问题讨论】:

    标签: ios swift uiimage phasset


    【解决方案1】:

    我很确定 mediaMetadata 不会有位置信息。使用这个CLLocation extension.

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

    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
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-02-11
      • 1970-01-01
      • 2022-06-12
      • 2017-02-01
      • 2019-10-19
      • 2014-07-09
      • 2011-08-19
      • 1970-01-01
      相关资源
      最近更新 更多