简体   繁体   English

CGBitmapContextCreate返回NULL

[英]CGBitmapContextCreate returns NULL

Under what circumstances will a CGBitmapContext fail to allocate? 在什么情况下CGBitmapContext无法分配? I have a table view, and it has multiple view options. 我有一个表视图,它有多个视图选项。 The user can see a small table cell with just previews, one larger preview per line, or two side by side previews per line. 用户可以看到只有预览的小表格单元格,每行一个更大的预览,或每行两个并排预览。 The first two render just fine, but the third one fails. 前两个渲染得很好,但第三个失败了。 There are no error messages from CGBitmapContextCreate, just errors after when I try to use it (ie invalid context 0x0). CGBitmapContextCreate没有错误消息,只是在我尝试使用它时出现错误(即无效的上下文0x0)。

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
//size is a passed parameter
CGContextRef c = CGBitmapContextCreate(NULL, size.width, size.height, 8, size.width*4, colorSpace, kCGImageAlphaNoneSkipLast);
CGColorSpaceRelease(colorSpace);

I am targeting iOS 5.0, building with 5.1. 我的目标是iOS 5.0,使用5.1构建。 The only difference between the working and non-working version is that the non-working version attempts to do it twice (size is small, less than 100x100). 工作版和非工作版之间的唯一区别是非工作版本尝试两次(尺寸小,小于100x100)。 Only the right side has this problem (ie the second attempt). 只有右侧有这个问题(即第二次尝试)。 The first attempt still works. 第一次尝试仍然有效。

如果size.width和/或size.height为0,则会发生这种情况。每次调用此方法时,请使用NSLog检查大小,以查看是否属于这种情况。

For posterity, this can also happen if you specify a 'bytesPerRow' that is not large enough to accommodate specified width. 对于后代,如果指定的“bytesPerRow”不足以容纳指定的宽度,也会发生这种情况。

Using Apple's sample code from https://developer.apple.com/library/ios/qa/qa1702/_index.html 使用来自https://developer.apple.com/library/ios/qa/qa1702/_index.html的 Apple示例代码

I noticed that CVPixelBufferGetWidth(), CVPixelBufferGetHeight(), and CVPixelBufferGetBytesPerRow() returned were returning 1280, 720, and 1960 respectively. 我注意到返回的CVPixelBufferGetWidth(),CVPixelBufferGetHeight()和CVPixelBufferGetBytesPerRow()分别返回1280,720和1960。 The 'bytesPerRow' value here (1960), is not wide enough to hold all the bytes for an image that is 1280 pixels wide. 这里的'bytesPerRow'值(1960),不足以容纳1280像素宽的图像的所有字节。 Seems like this is probably a bug in Apple's API. 似乎这可能是Apple的API中的一个错误。

So instead of using the value returned by CVPixelBufferGetBytesPerRow(), I used 'width * 4', and this worked just fine! 所以我没有使用CVPixelBufferGetBytesPerRow()返回的值,而是使用'width * 4',这很好用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM