使用Swift 2.2 OS X将元数据重写为.jpg文件



我正在尝试从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")"
        }

最新更新