【问题标题】:Custom UITableCell and exporting the TableCell as png file自定义 UITableCell 并将 TableCell 导出为 png 文件
【发布时间】:2013-02-01 12:52:00
【问题描述】:

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

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

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

谢谢

【问题讨论】:

    标签: iphone ios xcode ipad uitableview


    【解决方案1】:
    // 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];
    

    【讨论】:

    • 按下导出按钮后,它崩溃了。图片无法保存。这似乎是在PNG中保存图像的问题。还有其他选择吗?谢谢
    • 您遇到什么错误?我没有遇到崩溃。您可以尝试: [UIImageJPEGRepresentation(image, 1.0) writeToFile:dest atomically:YES];但我认为这不会产生影响,而且不会保留半透明背景。
    • 我收到了这个报告-[NSPathStore2 CGImage]: unrecognized selector sent to instance 0x9c236f0 2013-02-02 00:23:47.429 PictureList[59133:c07] *** 由于未捕获的异常而终止应用程序' NSInvalidArgumentException',原因:'-[NSPathStore2 CGImage]:无法识别的选择器发送到实例 0x9c236f0'
    • 我认为如果您将 NSString 输入到 UIImagePNGRepresentation 而不是 UIImage,就会发生这种情况。你能仔细检查你的代码,也许调试一下你的图像变量是否真的是一个 UIImage。
    • 谢谢。现在它起作用了。再问一个问题。如果我使用分组表格样式,我可以只选择两边没有空格的单元格吗?非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-17
    • 1970-01-01
    相关资源
    最近更新 更多