您的位置:首页 > 其它

<Error>: CGBitmapContextCreateImage: invalid context 0x0. 解决方案

2016-02-24 14:43 411 查看
阐述
最近研究 AVAssetReader+AVAssetReaderTrackOutput 
用其写自己的视频播放类。编写中CMSampleBufferRef转换
CGImageRef 的时候产生一些 Error,如下方:
<Error>: CGBitmapContextCreateImage: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental
variable.

<Error>: CGBitmapContextCreateImage: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context  and is thereby contributing to an overall degradation
of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.


相关代码
- (CGImageRef)imageFromSampleBuffer:(CMSampleBufferRef)sampleBuffer {
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
// Lock the base address of the pixel buffer
CVPixelBufferLockBaseAddress(imageBuffer, 0);
// Get the number of bytes per row for the pixel buffer
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
// Get the pixel buffer width and height
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);
//Generate image to edit
unsigned char* pixel = (unsigned char *)CVPixelBufferGetBaseAddress(imageBuffer);
CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();
CGContextRef context=CGBitmapContextCreate(pixel, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little|kCGImageAlphaPremultipliedFirst);
CGImageRef image = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
return image;
}


研讨

出现这类型错误,多半是创建内容画布的时候,height or width = 0, 因此在逻辑上添加判断(排除产生height or width
= 0 的情况),可以消除这种错误。

资料
1. http://stackoverflow.com/questions/19377299/cgcontextsavegstate-invalid-context
2. http://www.4byte.cn/question/425886/cgcontextsavegstate-invalid-context.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息