ios - 将自定义的 UITableViewCell 导出到 UIImage

标签 ios uitableview uiimage export

我有一个 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/

相关文章:

UITableView - iOS 7 中的透明背景

iphone - iOS:在自动布局下更改 UITableView 高度

ios - 在 Swift 中使用带有 IBOutlet 的 UIImageView 在 Interface Builder 中查看它

ios - UITableViewCell.ImageView.image 对比 UIImage

iphone - NSString之间可以插入图片吗?

iphone - iOS 中加速度计数据中的重力作为引用系

ios - 如果我在我的 iPad 应用程序中使用内置/标准 iOS 控件,它们会随着 iOS 系统更新自动更新吗?

iOS UITableViewCell cell.imageView 设置圆角

ios - Swift:HEIC 图像的 PHAssetChangeRequest 失败

ios - 核心数据迁移——表已存在