将 NSImage* 转换为 CGImageRef?

发布于 2024-08-27 16:10:50 字数 314 浏览 6 评论 0原文

有没有一种在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

喵星人汪星人 2024-09-03 16:10:51

此页面上找到了以下解决方案:

NSImage* someImage;
// create the image somehow, load from file, draw into it...
CGImageSourceRef source;

source = CGImageSourceCreateWithData((CFDataRef)[someImage TIFFRepresentation], NULL);
CGImageRef maskRef =  CGImageSourceCreateImageAtIndex(source, 0, NULL);

所有方法似乎都是10.4+,所以你应该没事。

Found the following solution on this page:

NSImage* someImage;
// create the image somehow, load from file, draw into it...
CGImageSourceRef source;

source = CGImageSourceCreateWithData((CFDataRef)[someImage TIFFRepresentation], NULL);
CGImageRef maskRef =  CGImageSourceCreateImageAtIndex(source, 0, NULL);

All the methods seem to be 10.4+ so you should be fine.

ゃ懵逼小萝莉 2024-09-03 16:10:51

【这是一条漫长的路。仅当您仍支持旧版本的 OS X 时才应执行此操作。如果您需要 10.6 或更高版本,只需使用 < code>CGImage 方法 代替。]

  1. 创建 CGBitmapContext。
  2. 为位图上下文创建一个 NSGraphicsContext。
  3. 将图形上下文设置为当前上下文。
  4. 绘制图像。
  5. 从位图上下文的内容创建 CGImage。
  6. 释放位图上下文。

[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.]

  1. Create a CGBitmapContext.
  2. Create an NSGraphicsContext for the bitmap context.
  3. Set the graphics context as the current context.
  4. Draw the image.
  5. Create the CGImage from the contents of the bitmap context.
  6. Release the bitmap context.
草莓酥 2024-09-03 16:10:51

以下是使用 - CGImageForProposedRect:context:hints: 的更详细答案:

NSImage *image = [NSImage imageNamed:@"image.jpg"];

NSRect imageRect = NSMakeRect(0, 0, image.size.width, image.size.height);

CGImageRef cgImage = [image CGImageForProposedRect:&imageRect context:NULL hints:nil];

Here's a more detailed answer for using - CGImageForProposedRect:context:hints::

NSImage *image = [NSImage imageNamed:@"image.jpg"];

NSRect imageRect = NSMakeRect(0, 0, image.size.width, image.size.height);

CGImageRef cgImage = [image CGImageForProposedRect:&imageRect context:NULL hints:nil];
讽刺将军 2024-09-03 16:10:51

从 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.

你的背包 2024-09-03 16:10:51

只是使用CGImageForProposedRect:context:hints:来做到这一点...

NSImage *image; // an image
NSGraphicsContext *context = [NSGraphicsContext currentContext];
CGRect imageCGRect = CGRectMake(0, 0, image.size.width, image.size.height);
NSRect imageRect = NSRectFromCGRect(imageCGRect);
CGImageRef imageRef = [image CGImageForProposedRect:&imageRect context:context hints:nil];

Just did this using CGImageForProposedRect:context:hints:...

NSImage *image; // an image
NSGraphicsContext *context = [NSGraphicsContext currentContext];
CGRect imageCGRect = CGRectMake(0, 0, image.size.width, image.size.height);
NSRect imageRect = NSRectFromCGRect(imageCGRect);
CGImageRef imageRef = [image CGImageForProposedRect:&imageRect context:context hints:nil];
空‖城人不在 2024-09-03 16:10:51

自 2021 年起,CALayers 内容可以直接使用 NSImage 提供,但您仍然可能会得到
[Utility] 不受支持的表面格式:LA08 警告。
经过几天的研究和测试,我发现如果您使用 backingLayer(又名 CALayer)创建 NSView 并且仅使用 NSImage 来提供其内容,则会触发此警告。仅此一点并不是什么大问题。但如果您尝试通过 [self.layer renderInContext:ctx] 渲染它,每次渲染都会再次触发警告。

为了使使用简单,我创建了一个 NSImage 扩展来解决这个问题..

@interface NSImage (LA08FIXExtension)
-(id)CGImageRefID;
@end

@implementation NSImage (LA08FIXExtension)

-(id)CGImageRefID {
    NSSize size = [self size];
    NSRect rect = NSMakeRect(0, 0, size.width, size.height);
    CGImageRef ref = [self CGImageForProposedRect:&rect context:[NSGraphicsContext currentContext] hints:NULL];
    return (__bridge id)ref;
}

@end

看看它如何不直接返回 CGImageRef 而是桥接转换为 id..!这可以解决问题。

现在你可以像使用它一样..

CALayer *somelayer = [CALayer layer];
somelayer.frame = ... 
somelayer.contents = [NSImage imageWithName:@"SomeImage"].CGImageRefID;

PS:发布她是因为如果你也遇到这个问题,谷歌无论如何都会引导你找到这个答案线程,并且所有上述答案都启发了我进行此修复。

As of 2021, CALayers contents can directly be feed with NSImage 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 its contents. 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..

@interface NSImage (LA08FIXExtension)
-(id)CGImageRefID;
@end

@implementation NSImage (LA08FIXExtension)

-(id)CGImageRefID {
    NSSize size = [self size];
    NSRect rect = NSMakeRect(0, 0, size.width, size.height);
    CGImageRef ref = [self CGImageForProposedRect:&rect context:[NSGraphicsContext currentContext] hints:NULL];
    return (__bridge id)ref;
}

@end

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..

CALayer *somelayer = [CALayer layer];
somelayer.frame = ... 
somelayer.contents = [NSImage imageWithName:@"SomeImage"].CGImageRefID;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文