title: CGContext & CGBitmapContextCreateImage errors, how to fix?
body: <p>I've been at this for hours now, and don't even know how to debug this error. Maybe there's an SO expert who knows what's going on.</p>
  
  <pre><code>- (void) prepareSubset {
  
  CGSize size = [image size];
  
  float scale = fminf(1.0f, fmaxf(SUBSET_SIZE / cropRect.size.width, SUBSET_SIZE / cropRect.size.height));
  CGPoint offset = CGPointMake(-cropRect.origin.x, -cropRect.origin.y);
  
  subsetWidth = cropRect.size.width * scale;
  subsetHeight = cropRect.size.height * scale;
  
  subsetBytesPerRow = ((subsetWidth + 0xf) &gt;&gt; 4) &lt;&lt; 4;
  subsetData = (unsigned char *)malloc(subsetBytesPerRow * subsetHeight);
  
  CGColorSpaceRef grayColorSpace = CGColorSpaceCreateDeviceGray();
  
  CGContextRef ctx = CGBitmapContextCreate(subsetData, subsetWidth, subsetHeight, 8, subsetBytesPerRow, grayColorSpace, kCGImageAlphaNone);
  CGColorSpaceRelease(grayColorSpace);
  CGContextSetInterpolationQuality(ctx, kCGInterpolationNone);
  CGContextSetAllowsAntialiasing(ctx, false);
  
  CGContextTranslateCTM(ctx, 0.0, subsetHeight);
  CGContextScaleCTM(ctx, 1.0, -1.0);  
  
  UIGraphicsPushContext(ctx);
  CGRect rect = CGRectMake(offset.x * scale, offset.y * scale, scale * size.width, scale * size.height);
  [image drawInRect:rect];
  UIGraphicsPopContext();
  
  CGContextFlush(ctx);
  
  CGImageRef subsetImageRef = CGBitmapContextCreateImage(ctx);
  
  self.subsetImage = [UIImage imageWithCGImage:subsetImageRef];
  CGImageRelease(subsetImageRef);
  
  CGContextRelease(ctx);
  </code></pre>
  
  <p>}</p>
  
  <p>These are the errors that come up once I call this method</p>
  
  <pre><code>&lt;Error&gt;: CGContextSetInterpolationQuality: invalid context 0x0
  &lt;Error&gt;: CGContextSetAllowsAntialiasing: invalid context 0x0
  &lt;Error&gt;: CGContextTranslateCTM: invalid context 0x0
  &lt;Error&gt;: CGContextScaleCTM: invalid context 0x0
  &lt;Error&gt;: CGContextSaveGState: invalid context 0x0
  &lt;Error&gt;: CGContextSetBlendMode: invalid context 0x0
  &lt;Error&gt;: CGContextSetAlpha: invalid context 0x0
  &lt;Error&gt;: CGContextTranslateCTM: invalid context 0x0
  &lt;Error&gt;: CGContextScaleCTM: invalid context 0x0
  &lt;Error&gt;: CGContextDrawImage: invalid context 0x0
  &lt;Error&gt;: CGContextRestoreGState: invalid context 0x0
  &lt;Error&gt;: CGContextFlush: invalid context 0x0
  &lt;Error&gt;: CGBitmapContextCreateImage: invalid context 0x0
  </code></pre>
  
  <p>Can anyone PLEASE tell me what's going on? Thanks in advance.</p>