开发者

Exporting customized UITableViewCells into UIImage

开发者 https://www.devze.com 2023-01-24 15:53 出处:网络
I have a UITableView with a set of UITableViewCells in it - each of the cells contain a UIView which shows some graphics in a co开发者_开发知识库ntext.

I have a UITableView with a set of UITableViewCells 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消