Rasterizing a UIView
I was doing some work with parsing and visualising SVG files but the views ended up having lots of paths and a lot of memory was consumed. So the idea was to convert the UIView to an image …
// Doing work to generate the SVG in the schematics view; // At the end there was no reason to keep all these (SVGView)subviews // ******* Rasterize ************ // Create an image from the view UIGraphicsBeginImageContext(schematicsView.frame.size); [schematicsView.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); // Remove the SVG views to release the memory. for (SVGView *view in SVGReferences) [view removeFromSuperview]; // Add the image that was created before as a subview [self.view addSubview:[[[UIImageView alloc] initWithImage:viewImage] autorelease]];
I swear pure awesomeness!!!