Tag: graphicscontext

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

我正在使用这个非常优雅的一段代码获取一个UIScrollView的屏幕截图,包括屏幕外的部分来捕获一个UITableView的全部内容导出到一个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设备(iphone 4s)上运行此代码时,在调用此方法时,出现以下错误: [self.controller.tableView.layer renderInContext: UIGraphicsGetCurrentContext()]; 错误: Error: CGContextTranslateCTM: invalid context 0x2924b0 Error: CGContextDrawImage: invalid context 0x2924b0 Error: CGContextRestoreGState: invalid […]

在iOS上,从上下文创build一个图层并获取图层的上下文之后,这些上下文又是如何相互关联的?

我们可以从当前的graphics上下文创build一个图层,然后获取图层的上下文: CGContextRef context = UIGraphicsGetCurrentContext(); CGLayerRef layer = CGLayerCreateWithContext(context, CGSizeMake(self.frame.size.width, self.frame.size.height), NULL); CGContextRef contextOfLayer = CGLayerGetContext(layer); 所以我们现在有两个上下文: context和contextOfLayer 。 这两种情况如何相互关联? contextOfLayer实际上是context一部分, context有一个图层上下文指针的数组? 如果我使用NSLog(@"%p", …)打印出他们的地址,他们有不同的地址,所以它们不是同一个对象。 而且我认为contextOfLayer不会影响上下文堆栈,所以它只是一个独立的上下文而已?