iPhone cannot create CGBitmapContext

*爱你&永不变心* 提交于 2019-12-10 14:47:44

问题


I have a problem with creating a CGBitmapContext.

CGContextRef bitmapContext = CGBitmapContextCreate(nil, imageSize.width, imageSize.height, 8, imageSize.width * 4, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaNoneSkipFirst)

When I run my application normally it works. But when I run it in my 'Test application' (a running app that will perform my tests) the context is logged as (null) and I get the following errors:

<Error>: CGContextSetFillColorWithColor: invalid context 0x0
<Error>: CGContextFillRects: invalid context 0x0
<Error>: CGContextDrawImage: invalid context 0x0
<Error>: CGContextDrawImage: invalid context 0x0
<Error>: CGBitmapContextCreateImage: invalid context 0x0

My application doesn't crash. But apparently there is something wrong. (I know the errors are created by calling those methods with a nil context).


回答1:


This error often appears when the size of the bitmap you're trying to create is CGSizeZero. Check the actual value of imageSize.

Besides, you leak the CGColorSpaceRef you pass into the function.




回答2:


Do you check for the CGBitmapContextCreate return value to be non-nil? You will get a nil value if there isn't enough memory available on the device to create the context.

How much memory are you using in the app and for what size bitmap are you asking?



来源:https://stackoverflow.com/questions/4438571/iphone-cannot-create-cgbitmapcontext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!