如何将NSCIImageRep绘制到NSView

时间:2012-01-27 13:01:28

标签: cocoa qtkit

我在绘制通过QTKit mCaptureDecompressedVideoOutput获取的NSCIImageRep时遇到了麻烦。

由于我不想使用OpenGL绘制图像,我试图将NSView子类化并在那里绘制图像:

- (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是一个指向我通过mCaptureDecompressedVideoOutput回调获得的CGImageRef的指针

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

此代码崩溃了我的机器。有什么建议?

1 个答案:

答案 0 :(得分:4)

您无需通过CGImageRef来绘制NSCIImageRep。只需要它CIImage并绘制:

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