iphone - 如何捕获 UITableView/UIScrollView 完整内容的 UIImage 并使其在 ios 设备上工作

标签 iphone ios uiscrollview screenshot graphicscontext

我正在使用这段非常优雅的代码 Getting a screenshot of a UIScrollView, including offscreen parts捕获要导出到 UIImage 的 UITableView 的全部内容。

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 设备 (iphone 4s) 上运行此代码时,调用此方法时出现以下错误:

[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...

当表格 View 内容大小在其范围内时,此代码运行良好,但一旦它大于其范围(即使是 1 个像素),它就会进入上述错误的无限循环。但这只发生在设备上。不是模拟器。

有什么想法吗?

最佳答案

事实证明,使用 [UIImage ..] resizableImageWithCapInsets] 方法并将其应用于单元格 backgroundView 并调整框架会导致在实际设备上而非模拟器上发生各种奇怪的错误。

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

关于iphone - 如何捕获 UITableView/UIScrollView 完整内容的 UIImage 并使其在 ios 设备上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10695258/

相关文章:

ios - iPhone 和 OpenCV

ios - 在 Swift 中使用正则表达式验证 RGB 字符串

ios - TableView Cell 视频自动播放

ios - XCODE/Swift Autolayout ScrollView with dynamic label, textview etc. 如何根据textview设置scrollview高度

iOS 嵌套 UIScrollViews 使用 AutoLayout

iphone - 如何在本地录制 iphone 中的电话?

iphone - iOS:Core Motion 用于检测远距离的较大运动?

ios - 将焦点添加到 Popover 中的 TextView

ios - 使用日期选择器时出错

ios - 自定义应用程序中的 UITableview 部分