将UITableViewCell导入PNG

将UITableViewCell导入PNG

问题描述:

要求用到UITableViewCell导入到一个图片,PNG或者其他格式,实现使用MFMailComposerViewController通过Email传递。请高手帮忙。

调用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;
}

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

可以存储图片

- (UIImage *)captureCell {

CGRect rect = [yourTableCell bounds];

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

}