cocoa – 如何将NSCIImageRep绘制到NSView

前端之家收集整理的这篇文章主要介绍了cocoa – 如何将NSCIImageRep绘制到NSView前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在绘制通过QTKit mCaptureDecompressedVideoOutput获得的NSCI ImageRep时遇到了麻烦.

由于我不想使用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`.

这段代码崩溃了我的机器.有什么建议?

解决方法

您不需要通过CGImageRef来绘制NSCIImageRep.只要问它CIImage并绘制:

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

猜你在找的cocoa相关文章