[英]Custom UITableCell and exporting the TableCell as png file
我想要一个应用程序,当我在某些表格单元格中输入大量数据时。 然后,所有数据将汇总到最低的表格单元格中。 (我已经做到了)。
我的问题是如何专门将最低或任何特定的表格单元导出到PNG格式中作为图像?
我尝试使用打印屏幕,但效果不是很好,并且设备数量有所不同。 有什么我只能导出该单元格的代码吗?
谢谢
// Specify your index path
NSUInteger index[] = {0, 1}; // section, row
NSIndexPath *indexPath = [[NSIndexPath alloc] initWithIndexes:index length:2];
// Get the cell
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
// Render a graphics context to turn your cell into a UIImage
CGSize imageSize = cell.bounds.size;
UIGraphicsBeginImageContext(imageSize);
CGContextRef imageContext = UIGraphicsGetCurrentContext();
[cell.layer renderInContext:imageContext];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Save the image as PNG
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
NSString *dest = [docDir stringByAppendingPathComponent:@"image.png"];
[UIImagePNGRepresentation(image) writeToFile:dest atomically:YES];
编辑
为了获得该单元格的更精确快照,可以使用该单元格的contentView属性,即
[cell.contentView.layer renderInContext:imageContext];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.