EXIF数据读写

时间:2016-10-21 11:12:05

标签: swift exif

我搜索了从图片文件中获取EXIF数据并将其写回Swift。但我只能找到不同语言的预定义库。

我还发现了对“CFDictionaryGetValue”的引用,但是我需要哪些密钥来获取数据?我该怎么写回来?

3 个答案:

答案 0 :(得分:17)

我正在使用它从图像文件获取 EXIF信息:

import ImageIO

let fileURL = theURLToTheImageFile
if let imageSource = CGImageSourceCreateWithURL(fileURL as CFURL, nil) {
    let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil)
    if let dict = imageProperties as? [String: Any] {
        print(dict)
    }
}

它为您提供了一个字典,其中包含各种信息,例如颜色配置文件 - EXIF信息具体位于dict["{Exif}"]

答案 1 :(得分:2)

雨燕4

extension UIImage {
    func getExifData() -> CFDictionary? {
        var exifData: CFDictionary? = nil
        if let data = self.jpegData(compressionQuality: 1.0) {
            data.withUnsafeBytes {(bytes: UnsafePointer<UInt8>)->Void in
                if let cfData = CFDataCreate(kCFAllocatorDefault, bytes, data.count) {
                    let source = CGImageSourceCreateWithData(cfData, nil)
                    exifData = CGImageSourceCopyPropertiesAtIndex(source!, 0, nil)
                }
            }
        }
        return exifData
    }
}

答案 2 :(得分:1)

您可以使用 AVAssetExportSession 来写入元数据。

let asset = AVAsset(url: existingUrl)
let exportSession = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetHighestQuality)
exportSession?.outputURL = newURL
exportSession?.metadata = [
  // whatever [AVMetadataItem] you want to write
]
exportSession?.exportAsynchronously {
  // respond to file writing completion
}