小编Heb*_*ian的帖子

用CGImage绘制图像?

我有一个CGImageRef,我想将它显示在 上NSView。我已经有一个CGImageRef源路径,但以下不起作用:

- (void)drawRect:(NSRect)rect {

  NSString *   thePath = [[NSBundle mainBundle] pathForResource: @"blue_pict"
                                                         ofType: @"jpg"];
  NSLog(@"the path : %@", thePath);

  CGImageRef myDrawnImage = [self createCGImageRefFromFile:thePath];

  NSLog(@"get the context");
  CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext]     graphicsPort];
  if (context==nil) {
    NSLog(@"context failed");
    return;
  }

  //get the bitmap context
  CGContextRef myContextRef = CreateARGBBitmapContext(myDrawnImage);

  //set the rectangle
  NSLog(@"get the size for imageRect");
  size_t w = CGImageGetWidth(myDrawnImage);
  size_t h = CGImageGetHeight(myDrawnImage);
  CGRect imageRect = {{0,0}, {w,h}};
  NSLog(@"W : %d", w); …
Run Code Online (Sandbox Code Playgroud)

cocoa core-graphics nsview quartz-2d

5
推荐指数
1
解决办法
9164
查看次数

标签 统计

cocoa ×1

core-graphics ×1

nsview ×1

quartz-2d ×1