How to draw a NSCIImageRep to an NSView

903 Views Asked by At

I am having trouble at drawing a NSCIImageRep which I obtain via a QTKit mCaptureDecompressedVideoOutput.

As I do not want to draw the image using OpenGL, I attempted to subclass a NSView and draw the image there:

- (void) drawRect:(NSRect)dirtyRect
{
    NSLog(@"DrawInRect");
    CGContextRef myContext = [[NSGraphicsContext currentContext] graphicsPort];

    if (imageRep != nil)
    {
       CGImageRef image = [imageRep CGImageForProposedRect: &dirtyRect context:    [NSGraphicsContext currentContext] hints:nil];
       CGContextDrawImage(myContext, dirtyRect, image);
       CGImageRelease(image);
    }  
}

imageRep is a pointer to the CGImageRef I obtain via the mCaptureDecompressedVideoOutput callback

- (void)captureOutput:(QTCaptureOutput *)captureOutput didOutputVideoFrame:(CVImageBufferRef)videoFrame withSampleBuffer:(QTSampleBuffer *)sampleBuffer fromConnection:(QTCaptureConnection *)connection`.

This code crashes my machine. Any suggestions?

1

There are 1 best solutions below

1
On BEST ANSWER

You don't need to go via CGImageRef just to draw an NSCIImageRep. Just ask it for a CIImage and draw that:

CIImage* anImage = [yourNSCIImageRep CIImage];
[anImage drawAtPoint:NSZeroPoint
            fromRect:NSRectFromCGRect([anImage extent])
           operation:NSCompositeSourceOver
            fraction:1.0];