0

我想要一个应用程序,当我在某些表格单元格中输入大量数据时。然后所有数据将汇总在最低的表格单元格中。(我已经这样做了)。

我的问题是如何将最低或任何特定的表格单元格作为图像专门导出为 PNG 格式?

我曾尝试使用打印屏幕,但它不是那么好,并且数量不同的设备。有没有我只能导出该单元格的代码?

谢谢

4

1 回答 1

4
// 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];
于 2013-02-01T13:43:04.237 回答