macos 从 CGImageRef 获取 NSImage
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9098388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting NSImage from CGImageRef
提问by MobileOverlord
I am trying process an image in CoreGraphics and then return the processed image back to an NSImage
for saving and displaying. I have ample resources on how to perform these functions in iOS but the helper methods seem to be missing in NSImage
. In iOS the class method is imageWithCGImage:
, how can you do this in Mac OS?
我正在尝试在 CoreGraphics 中处理图像,然后将处理后的图像返回给NSImage
用于保存和显示。我有关于如何在 iOS 中执行这些功能的充足资源,但NSImage
. 在 iOS 中,类方法是imageWithCGImage:
,你怎么能在 Mac OS 中做到这一点?
回答by Peter Hosey
The matching method in NSImage is initWithCGImage:size:
.
NSImage 中的匹配方法是initWithCGImage:size:
.
The second argument takes the image's size in points. The factor between the size in pixels (of the CGImage) and the size in points is the scale factor. So, for example, if you have a 100×100px CGImage, and pass a size of (NSSize){ 50.0, 50.0 }
, the image will be 50 points in size, and double-resolution.
第二个参数以点为单位获取图像的大小。以像素为单位的(CGImage 的)大小与以点为单位的大小之间的因素是比例因子。因此,例如,如果您有一个 100×100px 的 CGImage,并传递一个大小为(NSSize){ 50.0, 50.0 }
,则图像的大小将是 50 点,并且是双倍分辨率。
Usually you should just pass the size in pixels (from the CGImage) as the size in points. For handling multiple scale factors, it's better to use a single NSImage with multiple NSImageReps, like what you get from -[NSWorkspace iconForFileType:]
for most types or from creating an NSImage from a typical .icns file.
通常你应该只传递以像素为单位的大小(来自 CGImage)作为以点为单位的大小。为了处理多个比例因子,最好将单个 NSImage 与多个 NSImageReps 一起使用,就像您从-[NSWorkspace iconForFileType:]
大多数类型中获得的内容或从典型的 .icns 文件创建 NSImage 一样。