Export custom UITableViewCells to UIImage

I have a UITableView with a set of UITableViewCell in it - each of the cells contains a UIView that shows some graphs in context. What would be the best way to export these cells into one UIImage ?

thanks

edit 1: I know how to create an image from the table viewport, but this table scrolls from the screen, and I would like to create a UIImage all cells, not just the ones you see.

+2
ios export uitableview uiimage
source share
1 answer

You must do this by pointing the drawing view layer into a custom graphics context, and then creating a CGImage bitmap from that context. When you have a CGImage, you can create a UIImage from it. It would look something like this:

 // Create a bitmap context. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef bitmapContextForCell = CGBitmapContextCreate(nil, cell.bounds.size.width, cell.bounds.size.height, 8, 0, colorSpace, kCGImageAlphaNone); CGColorSpaceRelease(colorSpace); // Draw the cell layer into the context. [cell.layer renderInContext:bitmapContextForCell]; // Create a CGImage from the context. CGImageRef cgImageForCell = CGBitmapContextCreateImage(bitmapContextForCell); // Create a UIImage from the CGImage. UIImage * cellImage = [UIImage imageWithCGImage:cgImageForCell]; // Clean up. CGImageRelease(cgImageForCell); CGContextRelease(bitmapContextForCell); 

How to create an image for each cell. If you want to create a single image for all your cells, use a table instead of a cell.

+3
source share

All Articles