修改图像元数据

时间:2017-05-11 16:09:08

标签: swift core-foundation

我正在尝试修改JPEG图像中包含的元数据。它可以是图像中的任何元数据,在我的示例中,我尝试将DateTimeDigitized属性更改为当前日期。

我的代码似乎主要工作,但是set属性被删除而不是更改。我不确定为什么会发生这种情况,谁能告诉我我做错了什么?

我欢迎任何有助于执行任务的框架建议,但我特别感兴趣的是我对这种方法做错了。

我在Playground中运行此代码,其中名为“foo.jpg”的图像存储在路径~/Documents/Shared Playground Data/中。

import Foundation
import ImageIO            // CGImage functions
import PlaygroundSupport

let ImagePropertyExifDictionary = kCGImagePropertyExifDictionary as String
let ImagePropertyExifDateTimeDigitized = kCGImagePropertyExifDateTimeDigitized as String

func updateEXIFDateDigitized() {
  // Create URL for source and destination
  let sourceURL = playgroundSharedDataDirectory.appendingPathComponent("foo.jpg") as CFURL
  let destinationURL = playgroundSharedDataDirectory.appendingPathComponent("bar.jpg") as CFURL

  // Read source and get properties
  guard
    let sourceRef = CGImageSourceCreateWithURL(sourceURL, nil),
    var metadata = CGImageSourceCopyPropertiesAtIndex(sourceRef, 0, nil) as? [String:Any] else { return }

  print("unmodified properties", metadata, separator:"\n")

  // Modify EXIF DateTimeDigitized property
  guard var exif = metadata[ImagePropertyExifDictionary] as? [String:Any] else { return }

  exif[ImagePropertyExifDateTimeDigitized] = Date() as CFDate
  metadata[ImagePropertyExifDictionary] = exif as CFDictionary
  print("", "modified properties", metadata, separator:"\n")

  // Set up destination
  guard let destinationRef = CGImageDestinationCreateWithURL(destinationURL, kUTTypeJPEG, 1, nil) else { return }

  // Add image from source to destination with new properties
  CGImageDestinationAddImageFromSource(destinationRef, sourceRef, 0, metadata as CFDictionary)

  // Save destination
  guard CGImageDestinationFinalize(destinationRef) else { return }

  guard
    let sourceRef2 = CGImageSourceCreateWithURL(destinationURL, nil),
    let metadata2 = CGImageSourceCopyPropertiesAtIndex(sourceRef2, 0, nil) else { return }

  print("", "saved properties", metadata2, separator:"\n")
}

updateEXIFDateDigitized()

结果的相关位,我为了简洁而删除了其他字段:

unmodified properties
{
  "{Exif}" =     {
    DateTimeDigitized = "2007:07:31 17:42:01";
    DateTimeOriginal = "2007:07:31 17:42:01";
  };
}

modified properties
{
  "{Exif}" =     {
    DateTimeDigitized = "2017-05-11 15:45:38 +0000";
    DateTimeOriginal = "2007:07:31 17:42:01";
  };
}

saved properties
{
  "{Exif}" =     {
    DateTimeOriginal = "2007:07:31 17:42:01";
  };
}

1 个答案:

答案 0 :(得分:3)

我自己回答这个问题,因为我发现了为什么它没有保存数据,看起来这个问题可以帮助别人。

我的代码是正确的,唯一的问题是我没有正确格式化日期。由于日期格式不正确,因此框架会对其进行修剪。我将日期格式化为这样,并保存并正确显示:

let formatter = DateFormatter()
formatter.dateFormat = "yyyy:MM:dd HH:mm:ss"
exif[ImagePropertyExifDateTimeDigitized] = formatter.string(from: Date())

这取代了这条线:

exif[ImagePropertyExifDateTimeDigitized] = Date() as CFDate

现在输出(再次修剪为相关属性):

unmodified properties
  {
    "{Exif}" =     {
      DateTimeDigitized = "2007:07:31 17:42:01";
      DateTimeOriginal = "2007:07:31 17:42:01";
    };
}

modified properties
  {
    "{Exif}" =     {
      DateTimeDigitized = "2017:05:12 01:04:14";
      DateTimeOriginal = "2007:07:31 17:42:01";
    };
}

saved properties
  {
    "{Exif}" =     {
      DateTimeDigitized = "2017:05:12 01:04:14";
      DateTimeOriginal = "2007:07:31 17:42:01";
    };
}