从CGImageRef获取NSImage [英] Getting NSImage from CGImageRef

查看:301
本文介绍了从CGImageRef获取NSImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在CoreGraphics中处理图像,然后将处理后的图像返回到NSImage以进行保存和显示.我有足够的资源来了解如何在iOS中执行这些功能,但是NSImage中似乎缺少辅助方法.在iOS中,类方法为imageWithCGImage:,如何在Mac OS中做到这一点?

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?

推荐答案

NSImage中的匹配方法是initWithCGImage:size:.

The matching method in NSImage is initWithCGImage:size:.

第二个参数获取图像的大小(以磅为单位). (CGImage的)以像素为单位的大小和以磅为单位的大小之间的因子是比例因子.因此,例如,如果您有一个100×100px的CGImage,并且传递的大小为(NSSize){ 50.0, 50.0 },则该图像将为50个点,并且为双分辨率.

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)作为像素大小.为了处理多个比例因子,最好将单个NSImage与多个NSImageReps一起使用,例如大多数类型的-[NSWorkspace iconForFileType:]或从典型的.icns文件创建NSImage所得到的.

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.

这篇关于从CGImageRef获取NSImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆