我试图修改图像的元数据,但似乎原始图像的文件大小显着减少,这让我太烦了。如果图像大小为6MB,则修改后大小为3.2MB。我知道它很奇怪,但并非不可能。有没有人帮助我解决这个问题。我们将不胜感激。 这是我用swift 2.0编写的代码,工具是Xcode 7.3.1。
func writeProperties(url:NSURL, override: Bool) -> NSURL?{
guard let directory = url.URLByDeletingLastPathComponent else { return nil }
guard let base = url.URLByDeletingPathExtension?.lastPathComponent else { return nil }
guard let ext = url.pathExtension else { return nil }
let newName = override ? url.lastPathComponent! : "\(base)_modified.\(ext)"
let newPath = directory.URLByAppendingPathComponent(newName, isDirectory: false)
print("writeProperties to \(newPath)")
guard let imageSource = CGImageSourceCreateWithURL(url, nil) else { return nil }
let imageType = CGImageSourceGetType(imageSource)!
let data = NSMutableData()
guard let imageDestination = CGImageDestinationCreateWithData(data, imageType, 1, nil) else { return nil }
CGImageDestinationAddImageFromSource(imageDestination, imageSource, 0, saveProperties)
CGImageDestinationFinalize(imageDestination)
if let _ = try? data.writeToURL(newPath, options: NSDataWritingOptions.AtomicWrite) {
let alert = NSAlert()
alert.messageText = "Image Saved"
alert.informativeText = "Image saved to \(newPath.path!)"
alert.runModal()
return newPath
}
return nil
}