【发布时间】:2021-12-09 20:25:36
【问题描述】:
下面的代码我用来在 macOS 应用程序中捕获屏幕,
let img = CGDisplayCreateImage(CGMainDisplayID())
guard let destination = FileManager.default.urls(for: .downloadsDirectory,
in: .userDomainMask).first?.appendingPathComponent("shot.jpg", isDirectory: false)
else {
print("Unable to save captured image!")
return
}
let properties: CFDictionary = [
kCGImagePropertyPixelWidth: "900",
kCGImagePropertyPixelHeight: "380"
] as CFDictionary
if let dest = CGImageDestinationCreateWithURL(destination as CFURL, kUTTypeJPEG, 1, properties) {
CGImageDestinationAddImage(dest, img!, properties)
CGImageDestinationFinalize(dest)
}
else {
print("Unable to create captured image to the destination!")
}
我必须在保存时将图像缩放到特定大小。所以,我使用了CFDictionary 和图像的width, heigh 属性。但似乎我做错了。请帮我找出正确的解决方案。谢谢!
【问题讨论】:
-
提示:
NSImgedraw(in:from:operation:fraction:) -
@ElTomato 如果我将 CGImage 转换为 NSImage ,则 NSImage 容量的 JPEG 表示将高于存储中原始未缩放的图像容量。这就是问题所在。
-
“看来我做错了”并不能真正描述您面临的问题。如果您不想调整图像大小,那么您要解决的问题是什么?
-
@ElTomato,我正在寻找解决方案来调整图像大小,同时通过设置
properties或任何其他方式将其从 CGImage 中保存。 developer.apple.com/documentation/imageio/cgimageproperties/… -
再一次,“看来我做错了”并不能真正描述您面临的问题。