我有一个 UITableView
,其中包含一组 UITableViewCell
- 每个单元格都包含一个 UIView
,它在语境。
将这些单元格导出到一个 UIImage
中的最佳方法是什么?
谢谢
编辑1:我知道如何从表格的可视区域创建图像,但该表格滚动到屏幕之外,我想创建所有单元格的UIImage
,不仅仅是您看到的那些。
最佳答案
您应该能够通过告诉 View 层绘制到自定义图形上下文中,然后从该上下文创建位图 CGImage 来完成此操作。一旦你有了 CGImage,你就可以用它创建一个 UIImage。它看起来大概是这样的:
// 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);
这就是为每个单元格创建图像的方法。如果您想为所有单元格创建一张图像,请使用表格 View 而不是单元格。
关于ios - 将自定义的 UITableViewCell 导出到 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4168484/