某些 EXIF 元数据不会在 Swift 后更新
Some EXIF metadata won't update in Swift
我正在尝试更新图像的元数据,但我有好有坏。从我在博客和 Stack Overflow 上找到的示例来看,我认为我做的是正确的事情,但有些值会更新,而其他值则不会。谁能看出我哪里出错了?
func writeMetadata(originalImage url: URL) {
let newDestinationPath = NSTemporaryDirectory().appending("Out.jpg")
let newDestinationURL = URL(fileURLWithPath: newDestinationPath)
// Create source and destination
guard let imageData = try? Data(contentsOf: url),
let imageSource = CGImageSourceCreateWithData(imageData as CFData, nil),
let utf = CGImageSourceGetType(imageSource),
let destination = CGImageDestinationCreateWithURL(newDestinationURL as CFURL, utf, 1, nil) else { return }
// Get original metadata
guard let originalImageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) as? NSDictionary,
let mutableDictionary = originalImageProperties.mutableCopy() as? NSMutableDictionary,
let exifDictionary = mutableDictionary[kCGImagePropertyExifDictionary as String] as? NSMutableDictionary else { return }
print("Original metadata: \(originalImageProperties)")
// These properties successfully update
exifDictionary["CameraOwnerName"] = "ADB"
exifDictionary["ApertureValue"] = 1
// This property doesn't update
print("Date Before: \(exifDictionary[kCGImagePropertyExifDateTimeDigitized as String])")
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy:MM:dd HH:mm:ss"
exifDictionary[kCGImagePropertyExifDateTimeDigitized as String] = dateFormatter.string(from: Date())
print("Date After: \(exifDictionary[kCGImagePropertyExifDateTimeDigitized as String])")
// Update original metadata with updated Exif dictionary
mutableDictionary[kCGImagePropertyExifDictionary as String] = exifDictionary
// Write new file
CGImageDestinationAddImageFromSource(destination, imageSource, 0, mutableDictionary as CFDictionary)
CGImageDestinationFinalize(destination)
// Fetch updated metadata
guard let newSource = CGImageSourceCreateWithURL(newDestinationURL as CFURL, nil),
let newMetadata = CGImageSourceCopyPropertiesAtIndex(newSource, 0, nil) else { return }
print("Newly updated metadata: \(newMetadata)")
}
非常感谢任何指导!
我 运行 你的代码,运行 没问题,EXIF
数据按 DateTimeDigitized
的预期更新。您是否可能检查了 DateTimeOriginal
元数据或 {TIFF}
元数据,而您没有更新?
如果要设置 DateTimeOriginal
元数据,可以使用 kCGImagePropertyExifDateTimeOriginal
键按如下方式完成
exifDictionary[kCGImagePropertyExifDateTimeOriginal as String] = dateFormatter.string(from: Date())
我正在尝试更新图像的元数据,但我有好有坏。从我在博客和 Stack Overflow 上找到的示例来看,我认为我做的是正确的事情,但有些值会更新,而其他值则不会。谁能看出我哪里出错了?
func writeMetadata(originalImage url: URL) {
let newDestinationPath = NSTemporaryDirectory().appending("Out.jpg")
let newDestinationURL = URL(fileURLWithPath: newDestinationPath)
// Create source and destination
guard let imageData = try? Data(contentsOf: url),
let imageSource = CGImageSourceCreateWithData(imageData as CFData, nil),
let utf = CGImageSourceGetType(imageSource),
let destination = CGImageDestinationCreateWithURL(newDestinationURL as CFURL, utf, 1, nil) else { return }
// Get original metadata
guard let originalImageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) as? NSDictionary,
let mutableDictionary = originalImageProperties.mutableCopy() as? NSMutableDictionary,
let exifDictionary = mutableDictionary[kCGImagePropertyExifDictionary as String] as? NSMutableDictionary else { return }
print("Original metadata: \(originalImageProperties)")
// These properties successfully update
exifDictionary["CameraOwnerName"] = "ADB"
exifDictionary["ApertureValue"] = 1
// This property doesn't update
print("Date Before: \(exifDictionary[kCGImagePropertyExifDateTimeDigitized as String])")
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy:MM:dd HH:mm:ss"
exifDictionary[kCGImagePropertyExifDateTimeDigitized as String] = dateFormatter.string(from: Date())
print("Date After: \(exifDictionary[kCGImagePropertyExifDateTimeDigitized as String])")
// Update original metadata with updated Exif dictionary
mutableDictionary[kCGImagePropertyExifDictionary as String] = exifDictionary
// Write new file
CGImageDestinationAddImageFromSource(destination, imageSource, 0, mutableDictionary as CFDictionary)
CGImageDestinationFinalize(destination)
// Fetch updated metadata
guard let newSource = CGImageSourceCreateWithURL(newDestinationURL as CFURL, nil),
let newMetadata = CGImageSourceCopyPropertiesAtIndex(newSource, 0, nil) else { return }
print("Newly updated metadata: \(newMetadata)")
}
非常感谢任何指导!
我 运行 你的代码,运行 没问题,EXIF
数据按 DateTimeDigitized
的预期更新。您是否可能检查了 DateTimeOriginal
元数据或 {TIFF}
元数据,而您没有更新?
如果要设置 DateTimeOriginal
元数据,可以使用 kCGImagePropertyExifDateTimeOriginal
exifDictionary[kCGImagePropertyExifDateTimeOriginal as String] = dateFormatter.string(from: Date())