Cocoa 如何将NSCIImageRep绘制到NSView

Cocoa 如何将NSCIImageRep绘制到NSView,cocoa,qtkit,Cocoa,Qtkit,我在绘制NSCIImageRep时遇到问题,我通过QTKit MCaptureDecompressedDVideo输出获取NSCIImageRep 由于我不想使用OpenGL绘制图像,我尝试将NSView子类化并在其中绘制图像: - (void) drawRect:(NSRect)dirtyRect { NSLog(@"DrawInRect"); CGContextRef myContext = [[NSGraphicsContext currentContext] graphi

我在绘制NSCIImageRep时遇到问题,我通过QTKit MCaptureDecompressedDVideo输出获取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是指向我通过McAptureDecompressedDVideOutput回调获取的CGImageRef的指针

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

这个代码使我的机器崩溃。有什么建议吗

您不需要通过
CGImageRef
来绘制
NSCIImageRep
。只需向它索要一张
CIImage
,然后画出:

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

你能发布崩溃的堆栈跟踪吗?是的,这是我最终发现的。实际上,我也尝试将其转换为NSImage,但这导致在绘制时出现一些无法解释的内存泄漏。