How to get UIImage from CGContextRef?

I have a CGContextRef and I made my drawing material with a bitmap context.

Now I would like to get a function call to get the UIImage for the CGContextRef. How can I do it?

+7
source share
2 answers

Something like that:

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

Updated for Swift 3, this is a convenience feature that takes a CGContext and returns a UIImage. Note that you no longer need to free the context when you are done with it in Swift 3.

 func imageFromContext(_ context: CGContext) -> UIImage? { guard let cgImage = context.makeImage() else { return nil } return UIImage.init(cgImage: cgImage) } 
+2
source

All Articles