4

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?

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
Koffiman
  • 996
  • 1
  • 8
  • 18

1 Answers1

4

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];
Rob Keniger
  • 45,830
  • 6
  • 101
  • 134
  • 1
    Yes this is what I found out eventually. I did actually also try converting it to an NSImage, but that led to some unexplicable memory leaks when drawing. – Koffiman Jan 27 '12 at 21:52