如何将UITableViewCell导出到图像中?

时间:2012-12-03 10:57:44

标签: iphone objective-c ios uitableview printscreen

我需要将UITableViewCell导出为图像PNG或任何其他内容,以便我可以使用MFMailComposerViewController将其作为嵌入在电子邮件正文中的打印屏幕图像发送。

3 个答案:

答案 0 :(得分:2)

使用这些方法。只需致电UIImage *image = [cell screenshot];

- (UIImage *)screenshot
{
    return [self screenshotForRect:self.bounds];
}

- (UIImage *)screenshotForRect:(CGRect) rect
{
    UIGraphicsBeginImageContext(rect.size);
    [[UIColor clearColor] setFill];
    [[UIBezierPath bezierPathWithRect:rect] fill];
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [self.layer renderInContext:ctx];
    UIImage *anImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();    
    return anImage;
}

答案 1 :(得分:1)

尝试类似的事情:

UIGraphicsBeginImageContext(yourTableViewCellView.bounds.size);
[yourTableViewCellView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

然后您可以将图像保存到数据:)

答案 2 :(得分:1)

- (UIImage *)captureCell {

    //hide controls if needed
CGRect rect = [yourTableCell bounds];

    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [yourTableCell.layer renderInContext:context];   
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;

}