我有我的CGImageRef,我想通过我的NSView显示它。但是这段代码似乎不起作用,我已经从源代码路径中获得了CGImageRef。这里是我的代码:使用CGImage绘制图像?
- (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);
myDrawnImage = CGBitmapContextCreateImage(myContextRef);
NSLog(@"now draw it");
CGContextDrawImage(context, imageRect, myDrawnImage);
char *bitmapData = CGBitmapContextGetData(myContextRef);
NSLog(@"and release it");
CGContextRelease(myContextRef);
if (bitmapData) free(bitmapData);
CGImageRelease(myDrawnImage);
}
什么不好的代码?
- 感谢&方面 -
感谢您的快速回复。 因此,我应该将生成的CGImage更改为NSImage以便将其绘制到NSView中?所以然后我可以使用通用绘制方法,如 [myNSImage drawInRect:fromRect:operation:fraction] – Hebbian 2010-08-06 14:16:54
您可以这样做,是的。除非你不需要图像作为Core Graphics图像,否则我会建议你使用NSImage来完成你的任务。 – SteamTrout 2010-08-06 14:26:59