我正在尝试从 JPG 文件重写元数据。我想在元数据中添加一个关键字。 Xcode 没有给出任何错误,但文件未更改。 这是我的代码:
var pathToOpenFile:NSURL?
接下来我将文件路径写入变量“pathToOpenFile”。 如果用户将 ENTER 按钮插入 NSTextField,则工作操作:
@IBAction func endEditKeys(sender: AnyObject) {
if (pathToOpenFile != nil) {
let imageSource = CGImageSourceCreateWithURL(pathToOpenFile!, nil)
let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource!, 0, nil)! as NSDictionary;
let exifDict = imageProperties.valueForKey("{IPTC}") as! NSDictionary;
var Keywords:[String] = exifDict.valueForKey("Keywords") as! [String];
Keywords.append("ANY")
exifDict.setValue(Keywords, forKey: "Keywords")
let type = CGImageSourceGetType(imageSource!)
let count = CGImageSourceGetCount(imageSource!)
let mutableData = NSMutableData(contentsOfURL: pathToOpenFile!)
let destination = CGImageDestinationCreateWithData(mutableData!, type!, count, nil)
let removeExifProperties: CFDictionary = exifDict
for i in 0..<count {
CGImageDestinationAddImageFromSource(destination!, imageSource!, i, removeExifProperties)
}
CGImageDestinationFinalize(destination!)
}
}
你能帮我吗,为什么它不起作用(不更改元数据)?谢谢!
最佳答案
谢谢大家! 它正在工作,最后只需要重写 .JPG 文件。
if let _ = try? mutableData!.writeToURL(pathToOpenFile!, options: NSDataWritingOptions.AtomicWrite) {
// if you need, do anything. For example "print ("Savig file")"
}
关于swift - 使用 Swift 2.2 OS X 将元数据重写为 .jpg 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38512791/