2013-04-26 43 views
1

經過以下代碼塊的一些運行後,我得到CGContextDrawImage的無效上下文0x0。此代碼位於UIViewController中的用戶定義的方法中。我基本上使用AVFoundation拍攝靜止圖像的圖像,然後拍攝該圖像以讀取像素十六進制數據值。下面是我寫的代碼:無效上下文0x0 CGContextDrawImage

AVCaptureConnection *videoConnection = nil; 
for (AVCaptureConnection *connection in stillImageOutput.connections) 
{ 
    for (AVCaptureInputPort *port in [connection inputPorts]) 
    { 
     if ([[port mediaType] isEqual:AVMediaTypeVideo]) 
     { 
      videoConnection = connection; 
      break; 
     } 
    } 
    if (videoConnection) { break; } 
} 
[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error) 
{ 
    CFDictionaryRef exifAttachments = CMGetAttachment(imageSampleBuffer, kCGImagePropertyExifDictionary, NULL); 
    NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer]; 
    image2 = [[UIImage alloc] initWithData:imageData]; 
}]; 

CGImageRef imageRef = [image CGImage]; 
CGContextRef context = CGBitmapContextCreate(rawData, width, height,bitsPerComponent,bytesPerRow, colorSpace,kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); 
CGColorSpaceRelease(colorSpace); 
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef); 

爲什麼我得到CGContextDrawImage的無效上下文0x0?

回答

2

CGBitmapContextCreate如果無法創建請求的上下文,則返回NULLCGContextDrawImage主要是抱怨contextNULL

+0

我該如何讓它不是NULL?像有沒有辦法強制CGBitmapContextCreate總是返回一個有效的上下文? – David 2013-04-26 18:39:53

+0

正在傳入的一個或多個參數必須是正確的。如果文檔返回「NULL」,文檔沒有真正說明要查找什麼,只是無法創建上下文。我會說在那條線上設置斷點,看看參數是什麼與你期望傳遞的是什麼。 – 2013-04-26 19:36:51

+0

看起來image2是無效的。爲什麼會造成問題? – David 2013-04-26 21:15:11

相關問題