【问题标题】:invalid context 0x0 CGContextDrawImage无效的上下文 0x0 CGContextDrawImage
【发布时间】:2013-04-27 02:31:55
【问题描述】:

在运行以下代码块后,我得到了 CGContextDrawImage 的无效上下文 0x0。此代码位于 UIViewController 内的用户定义方法中。我基本上使用 AVFoundation 拍摄静止图像来捕获图像,并拍摄该图像以读取像素十六进制数据值。下面是我写的代码:

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?

【问题讨论】:

    标签: ios objective-c ios6


    【解决方案1】:

    CGBitmapContextCreate 如果无法创建请求的上下文,则返回NULL。 CGContextDrawImage 本质上是在抱怨 context 是 NULL。

    【讨论】:

    • 我怎样才能让它不为NULL?像有没有办法强制 CGBitmapContextCreate 总是返回一个有效的上下文?
    • 传入的一个或多个参数不能正确。如果它返回NULL,文档并没有真正说明要寻找什么,只是它无法创建上下文。我会说在那条线上设置断点,看看参数是什么与你期望传入的参数是什么。
    • 看来 image2 无效。为什么会导致问题?
    • image2 是异步设置的 (captureStillImageAsynchronously...),但您无需等待操作完成。根据执行此操作的上下文,解决方案可能就像将最后四行移动到异步操作的完成处理程序中一样简单,但如果您需要在当前方法退出之前让它变得清晰,那么您可能有阻塞直到异步工作完成,等等。
    • 感谢,将四行移到完成处理程序后,它似乎已修复。在它现在所处的状态下到底发生了什么,而不是在处理程序块中?
    猜你喜欢
    • 1970-01-01
    • 2013-10-30
    • 2012-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多