【问题标题】:Save (override) my jpg file with editing EXIF data通过编辑 EXIF 数据保存(覆盖)我的 jpg 文件
【发布时间】:2021-03-13 07:34:48
【问题描述】:

我打开并编辑我的 jpg 文件中的 EXIF(或 TIFF...)数据。 请帮助我用编辑数据保存(覆盖)我的 jpg 文件。 使用这个简单的代码:

let fileURL = URL(fileURLWithPath: "/Users/test/Pictures/IMG_2808.jpg")

if let imageSource = CGImageSourceCreateWithURL(fileURL as CFURL, nil) {
    var imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) as Dictionary?
    let exifDict = imageProperties?[kCGImagePropertyExifDictionary]

    if let UserComment = exifDict?[kCGImagePropertyExifUserComment] ?? nil {
        print("kCGImagePropertyExifUserComment: \(UserComment)")
    }
    else {
        exifDict!.setValue("My comment...", forKey: kCGImagePropertyExifUserComment as String)
        imageProperties![kCGImagePropertyExifDictionary] = exifDict
        
//      What I should do in the next step for update and override my jpg file on disk?
//      Maybe this is not nice way for this realisation? Please help
//
    }
}

【问题讨论】:

标签: swift image save jpeg exif


【解决方案1】:

经过一番测试,我找到了解决方案:

let fileURL = URL(fileURLWithPath: "/Users/test/Pictures/IMG_2808.jpg")

if let imageSource = CGImageSourceCreateWithURL(fileURL as CFURL, nil) {
    var imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) as Dictionary?
    let exifDict = imageProperties?[kCGImagePropertyExifDictionary]

    if let UserComment = exifDict?[kCGImagePropertyExifUserComment] ?? nil {
        print("kCGImagePropertyExifUserComment: \(UserComment)")
    }
    else {
        exifDict!.setValue("My comment...", forKey: kCGImagePropertyExifUserComment as String)
        imageProperties![kCGImagePropertyExifDictionary] = exifDict
     
        if let imageDestination = CGImageDestinationCreateWithURL(fileURL as CFURL, kUTTypeJPEG, 1, nil) {

            CGImageDestinationAddImageFromSource(imageDestination, imageSource, 0, imageProperties as CFDictionary?)

            CGImageDestinationFinalize(imageDestination)
        }
    }
}

我不知道这个解决方案是否正确,但它确实有效! 如果您知道更完善的方式,请发表评论

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-28
    • 1970-01-01
    • 2016-01-18
    • 2018-05-22
    • 2013-07-01
    • 1970-01-01
    • 2013-07-06
    相关资源
    最近更新 更多