title: Create gradient image programmatically in iOS
body: <p>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:</p>
  
  <pre><code>int *outBuf = malloc(sizeof(int) * width * height);
  for (int i = 0; i &lt; 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);
  </code></pre>
  
  <p>This should create a gradient from the very first to the very last pixel, but it results in a weird graphic:</p>
  
  <p><img src="https://i.stack.imgur.com/DzRFX.png" alt="generated image"></p>
  
  <p>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.</p>