本文介绍了如何捕获UITableView / UIScrollView的完整内容的UIImage并使其在ios设备上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用这段非常优雅的代码,用于捕获要导出到UIImage的UITableView的全部内容。

I am using this wonderfully elegant piece of code Getting a screenshot of a UIScrollView, including offscreen parts to capture the entire contents of a UITableView to be exported to a UIImage.

UIGraphicsBeginImageContextWithOptions(self.controller.tableView.contentSize, YES, 0);
{
    CGPoint savedContentOffset = self.controller.tableView.contentOffset;
    CGRect savedFrame = self.controller.tableView.frame;

    self.controller.tableView.contentOffset = CGPointZero;
    self.controller.tableView.frame = CGRectMake(0, 0, self.controller.tableView.contentSize.width, self.controller.tableView.contentSize.height);

    [self.controller.tableView.layer renderInContext: UIGraphicsGetCurrentContext()];
    image = UIGraphicsGetImageFromCurrentImageContext();

    self.controller.tableView.contentOffset = savedContentOffset;
    self.controller.tableView.frame = savedFrame;
}

UIGraphicsEndImageContext();

然而,当我在iOS设备上运行此代码时,它在模拟器中效果非常好4s)当调用此方法时,我得到以下错误的全部飞跃:

It works fantastically well in the simulator, however, when I run this code on an iOS device (iphone 4s) i get a whole leap of the following errors when this method is called:

[self.controller.tableView.layer renderInContext: UIGraphicsGetCurrentContext()];

错误:

Error: CGContextTranslateCTM: invalid context 0x2924b0
Error: CGContextDrawImage: invalid context 0x2924b0
Error: CGContextRestoreGState: invalid context 0x2924b0
Error: CGContextSaveGState: invalid context 0x2924b0
Error: CGContextScaleCTM: invalid context 0x2924b0
Error: CGContextClipToRect: invalid context 0x2924b0
etc...

当表格视图内容大小在其范围内时,此代码很有效但只要它大于其边界(即使是1个像素),它就会进入上面的无限循环错误。但这只发生在设备上。不是模拟器。

This code works well when the table views content size is within its bounds but as soon as it is greater than its bounds (even by 1 pixel) it goes into an endless loop of the above errors. But this only happens on the device. Not the simulator.

任何想法?

推荐答案

事实证明使用[UIImage ..] resizableImageWithCapInsets]方法并将其应用于单元backgroundView并调整框架会导致各种时髦的不良情况发生在实际设备上而不是模拟器上。

It turns out that using the [UIImage ..] resizableImageWithCapInsets] method and applying it to the cell backgroundView and adjusting the frame causes all sorts of funky badness to happen on actual devices but not the simulator.

恢复到已弃用的[UIImage ..] stretchableImageWithLeftCapWidth:topCapHeight:]方法解决了它。

Reverting back to the deprecated [UIImage ..] stretchableImageWithLeftCapWidth: topCapHeight:] method solved it.

这篇关于如何捕获UITableView / UIScrollView的完整内容的UIImage并使其在ios设备上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 13:00