I’m trying to process some imagedata which comes directly from the device camera. In order to understand the CGImage structure, I’m using a buffer to create an image which doesn’t work right for me (I know that I shouldn’t use C code within Obj-C Code, it’s just for understanding). The code is as follows:
int *outBuf = malloc(sizeof(int) * width * height);
for (int i = 0; i < width * height;i ++)
{
outBuf[i] = (((float)i) / ((float) width * height)) * 255.f;
}
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
CGContextRef ctx = CGBitmapContextCreate(outBuf, width, height, 8, width * 4, colorSpace, kCGImageAlphaNone);
CGImageRef img = CGBitmapContextCreateImage(ctx);
CFRelease(ref);
CGContextRelease(ctx);
free(outBuf);
CGColorSpaceRelease(colorSpace);
[view.imgView setImage: [UIImage imageWithCGImage:img]];
CGImageRelease(img);
This should create a gradient from the very first to the very last pixel, but it results in a weird graphic:

Where do the horizontal spacings come from? (I want to solve this issue, I’m aware that there are other possibilities to draw the gradient) Thanks in advance.
I’ve found the solution:
I was initialising the buffer with int*. Every int is 4 bytes long, but a grayscale picture just needs 1 byte per pixel. Changing the buffer to char* and modifying the CGBitmapContextCreate parameters fixed the issue: