tags:

views:

54

answers:

2

I have my CGImageRef and i want to display it trough my NSView. But this code doesn't seem to work, i already have an CGImageRef from source path. Here's my code :

- (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);

}

Is anything wrong with the codes?

  • Thanks & Regard -
+1  A: 

Yes, you don't actually draw the image. All you need to do is use CGContextDrawImage instead of creating an empty bitmap context.

Steam Trout
Thanks for the quick reply.So, should i change my generated CGImage into NSImage in order to draw it into NSView? So then i can use the common draw method like,[myNSImage drawInRect:fromRect:operation:fraction]
Hebbian
You can do that, yes. Unless you don't need image to be specifically a Core Graphics image I would suggest you to use NSImage for your task.
Steam Trout
A: 
CGImageRef myDrawnImage = [self createCGImageRefFromFile:thePath];

Now you have your image.

CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext]     graphicsPort];

Now you have your view's context. You have everything you need to draw the image.

CGContextRef myContextRef = CreateARGBBitmapContext(myDrawnImage);

Wait, what?

myDrawnImage = CGBitmapContextCreateImage(myContextRef);

O…kay… now you have captured the contents of a context that nothing has drawn in, forgetting about (and leaking) your loaded image by replacing it with a blank image.

CGContextDrawImage(context, imageRect, myDrawnImage);

You draw the blank image.

Cut out the creation of a bitmap context and the creation of an image of the contents of that context, and just draw the image you loaded into the context for your view.

Or use NSImage. That would be a two-liner.

Peter Hosey
Thanks for the reply Peter.Yes, the myDrawnImage = CGBitmapContextCreateImage(myContextRef); is just replacing my bitmap with new blank image (my bad). I cut it out and just draw it on the context directly, then display it as NSImage on NSView. Now my code has works smoothly.
Hebbian