簡體   English   中英

如何將UITableViewCell導出到圖像中?

[英]How can I export UITableViewCell into image?

我需要將UITableViewCell導出為圖像PNG或其他任何格式,以便可以使用MFMailComposerViewController將其作為嵌入在電子郵件正文中的打印屏幕圖像發送。

使用這些方法。 只需調用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 {

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

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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM