将定制的UITableViewCell导出到UIImage [英] Exporting customized UITableViewCells into UIImage

查看:178
本文介绍了将定制的UITableViewCell导出到UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UITableView ,其中包含一组 UITableViewCell ,每个单元格包含一个 UIView ,它在上下文中显示一些图形。
将这些单元格导出到一个 UIImage 中的最佳方法是什么?

I have a UITableView with a set of UITableViewCells in it - each of the cells contain a UIView which shows some graphics in a context. What would be the best way to export these cells into one UIImage?

谢谢

编辑1:我知道如何从表的可见区域创建一个图像,但是这个表滚动出屏幕,我想创建一个不仅仅是您看到的单元格。

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.

推荐答案

你应该能够通过告诉视图的图层绘制自定义图形上下文,然后从该上下文创建一个位图CGImage来做到这一点。一旦你有一个CGImage,你可以从它创建一个UIImage。它看起来大致如下:

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.

这篇关于将定制的UITableViewCell导出到UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆