代码之家  ›  专栏  ›  技术社区  ›  Brian Postow

将nsImage*转换为cImageRef?

  •  15
  • Brian Postow  · 技术社区  · 15 年前

    有没有一种简单的方法可以在10.5中工作?

    在10.6中,我可以使用nsImage cImageForProposedRect:空上下文:空提示:空

    如果我不使用1b黑白图像(如第4组TIFF),我可以使用位图,但cgBitmaps似乎不喜欢这个设置…有没有一个一般的方法可以做到这一点?

    我需要这样做,因为我有一个ikImageView,似乎只想添加cgImages,但我所得到的只是nsImages。目前,我正在使用一个私有的setimage:(nsImage*)方法,我真的不想使用它…

    5 回复  |  直到 10 年前
        1
  •  28
  •   pheelicks    15 年前

    在上找到以下解决方案 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);
    

    所有的方法都是10.4+,所以你应该很好。

        2
  •  17
  •   Community CDub    8 年前

    [这是很长的路。仅当您仍支持旧版本的OS X时才应执行此操作。如果您可以要求10.6或更高版本, just use the CGImage method 取而代之。

    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. 释放位图上下文。
        3
  •  16
  •   Peter Hosey    10 年前

    至于雪豹,你可以 ask the image to create a CGImage for you ,通过发送nsimage A CGImageForProposedRect:context:hints: 消息。

        4
  •  14
  •   Kane Cheshire    10 年前

    下面是一个更详细的使用方法 - 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];
    
        5
  •  1
  •   simioliolio    10 年前

    只是用 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];