2012-01-27 93 views
4

我在繪製通過QTKit mCaptureDecompressedVideoOutput獲取的NSCIImageRep時遇到了問題。如何繪製NSCIImageRep到NSView

因爲我不想畫使用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是一個指向CGImageRef我通過mCaptureDecompressedVideoOutput回調獲得

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

此代碼崩潰我的機器。有什麼建議麼?

+0

你可以從崩潰發佈堆棧跟蹤? – 2012-01-27 13:11:43

回答

4

您不需要通過CGImageRef去畫一個NSCIImageRep。只要問一個CIImage並得出:

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

是的,這是我最終發現的。我其實也嘗試將其轉換爲NSImage,但是在繪圖時會導致一些無法解釋的內存泄漏。 – Koffiman 2012-01-27 21:52:39