I have a UITableView
with a set of UITableViewCell
s in it - each of the cells contain a UIView
which shows some graphics in a co开发者_开发知识库ntext.
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 viewable area of the table, but this table scrolls out of the screen and I would like to create a UIImage
of all of the cells, not only those you see.
You should be able to do this by telling the view's layer to draw into a custom graphics context, then creating a bitmapped CGImage from that context. Once you have a CGImage, you can create a UIImage from it. It would look roughly 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's 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);
That's how to create a image for each cell. If you want to create one image for all your cells, use your table view instead of the cell.
精彩评论