I have an UIView where I was drawing some stuff inside -drawRect:. Now I need a CGImageRef from this graphics context or bitmap of the UIView. Is there an easy way to get that?
Like this (typed from memory, so it might not be 100% correct):
// Get a UIImage from the view's contents
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, view.contentScaleFactor);
CGContextRef context = UIGraphicsGetCurrentContext();
[view.layer renderInContext:context];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Convert UIImage to CGImage
CGImageRef cgImage = image.CGImage;
Also make sure that you add
#import <QuartzCore/QuartzCore.h>
to your code
来源:https://stackoverflow.com/questions/3777497/how-to-obtain-a-cgimageref-from-the-content-of-an-uiview