将 NSImage* 转换为 CGImageRef?
有没有一种在 10.5 中有效的简单方法?
在 10.6 中,我可以使用 nsImage CGImageForProposedRect: NULL context: NULL 提示:NULL
如果我不使用 1b 黑白图像(如第 4 组 TIFF),我可以使用位图,但 cgbitmaps 似乎不喜欢这种设置...是有一个通用的方法可以做到这一点吗?
我需要这样做是因为我有一个 IKImageView 似乎只想添加 CGImages,但我所拥有的只是 NSImages。目前,我正在使用私有 setImage:(NSImage*) 方法,我真的不想使用它......
Is there an easy way to do this that works in 10.5?
In 10.6 I can use nsImage CGImageForProposedRect: NULL context: NULL hints: NULL
If I'm not using 1b black and white images (Like Group 4 TIFF), I can use bitmaps, but cgbitmaps seem to not like that setup... Is there a general way of doing this?
I need to do this because I have an IKImageView that seems to only want to add CGImages, but all I've got are NSImages. Currently, I'm using a private setImage:(NSImage*) method that I'd REALLY REALLY rather not be using...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在此页面上找到了以下解决方案:
所有方法似乎都是10.4+,所以你应该没事。
Found the following solution on this page:
All the methods seem to be 10.4+ so you should be fine.
【这是一条漫长的路。仅当您仍支持旧版本的 OS X 时才应执行此操作。如果您需要 10.6 或更高版本,只需使用 < code>CGImage 方法 代替。]
[This is the long way around. You should only do this if you're still supporting an old version of OS X. If you can require 10.6 or later, just use the
CGImage
method instead.]以下是使用
- CGImageForProposedRect:context:hints:
的更详细答案:Here's a more detailed answer for using
- CGImageForProposedRect:context:hints:
:从 Snow Leopard 开始,您可以要求图像为您创建 CGImage,方法是向 NSImage 发送一个
CGImageForProposedRect:context:hints:
信息。As of Snow Leopard, you can just ask the image to create a CGImage for you, by sending the NSImage a
CGImageForProposedRect:context:hints:
message.只是使用
CGImageForProposedRect:context:hints:
来做到这一点...Just did this using
CGImageForProposedRect:context:hints:
...自 2021 年起,
CALayers
内容可以直接使用NSImage
提供,但您仍然可能会得到[Utility] 不受支持的表面格式:LA08
警告。经过几天的研究和测试,我发现如果您使用 backingLayer(又名 CALayer)创建 NSView 并且仅使用
NSImage
来提供其内容
,则会触发此警告。仅此一点并不是什么大问题。但如果您尝试通过 [self.layer renderInContext:ctx] 渲染它,每次渲染都会再次触发警告。为了使使用简单,我创建了一个 NSImage 扩展来解决这个问题..
看看它如何不直接返回 CGImageRef 而是桥接转换为
id
..!这可以解决问题。现在你可以像使用它一样..
PS:发布她是因为如果你也遇到这个问题,谷歌无论如何都会引导你找到这个答案线程,并且所有上述答案都启发了我进行此修复。
As of 2021,
CALayers
contents can directly be feed withNSImage
but you still may get an[Utility] unsupported surface format: LA08
warning.After a couple of days research and testing around i found out that this Warning is triggered if you created an NSView with backingLayer, aka CALayer and just used an
NSImage
to feed itscontents
. This alone is not much of a problem. But if you try to render it via[self.layer renderInContext:ctx]
, each rendering will trigger the warning again.To make use simple, i created an NSImage extension to fix this..
see how it does not return an CGImageRef directly but a bridged cast to
id
..! This does the trick.Now you can use it like..
PS: posted her because if you got that problem also, google will lead you to this Thread of answers anyway, and all above answers inspired me for this fix.