How do I get UIImage from a CGContextRef?

samwize picture samwize · May 3, 2011 · Viewed 13.2k times · Source

I have a CGContextRef and I did my drawing stuff with the bitmap context.

Now, I would like to have a function call to get a UIImage for the CGContextRef. How do I do that?

Answer

Nyx0uf picture Nyx0uf · May 3, 2011

Something like this :

-(UIImage*)doImageOperation
{
  // Do your stuff here
  CGImageRef imgRef = CGBitmapContextCreateImage(context);
  UIImage* img = [UIImage imageWithCGImage:imgRef];
  CGImageRelease(imgRef);
  CGContextRelease(context);
  return img;
}