Search code examples
ioscgcontext

CGBitmapContextGetData returning nothing


I want to get pixel information for the context

Debugger display

I don't know why every return is empty


Solution

  • The screenshot shows that the code is working fine but the data is treated as character data, which it is not.

    Since the variable bitmapData is declared as char*, the debugger treats it as a null-terminated C string. It displays: "". This indicates that bitmapData is NOT null, but starts with a null character.

    The raw pixel data is not a string, and not character data. So strlen() cannnot be used. And it's not possibly to display it with printf("%c").

    bitmapData is just a pointer to the raw bitmap data. Given bitmapData, it is not possible to derive the length of the data. The only safe way to query the length of the data is to call:

        int length = CGBitmapContextGetBytesPerRow(context) * CGBitmapContextGetHeight(context);
    

    Since you create a bitmap using 32 bits per pixel, I recommend to declare bitmapData as uint32_t*. Then each element in the array corresponds to a pixel.