【发布时间】:2013-02-01 12:52:00
【问题描述】:
我想要一个应用程序,当我在某些表格单元格中输入大量数据时。然后所有数据将汇总在最低的表格单元格中。 (我已经这样做了)。
我的问题是如何将最低或任何特定表格单元格作为图像专门导出为 PNG 格式?
我尝试过使用打印屏幕,但它不是那么好,而且数量不同。有没有我只能导出该单元格的代码?
谢谢
【问题讨论】:
标签: iphone ios xcode ipad uitableview
我想要一个应用程序,当我在某些表格单元格中输入大量数据时。然后所有数据将汇总在最低的表格单元格中。 (我已经这样做了)。
我的问题是如何将最低或任何特定表格单元格作为图像专门导出为 PNG 格式?
我尝试过使用打印屏幕,但它不是那么好,而且数量不同。有没有我只能导出该单元格的代码?
谢谢
【问题讨论】:
标签: iphone ios xcode ipad uitableview
// 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];
【讨论】: