获取UIScrollView的屏幕截图,包括屏幕外部分我有一个UIScrollViewDeendent,它实现了如下所示的获取Screenshot方法:-(void)takeScreenshot {
CGRect contextRect = CGRectMake(0, 0, 768, 1004);
UIGraphicsBeginImageContext(contextRect.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// do something with the viewImage here.}这基本上移动到滚动视图的顶部,并获取可见区域的屏幕截图。当iPad是面向人像的时候,它工作得很好,但是当它在风景中的时候,图像的底部被切断了(因为可见区域的高度只有748,而不是1004)。是否可以获得UIScrollView包括没有出现在屏幕上的区域?还是我需要向下滚动,拍第二张照片,把它们缝在一起?
3 回答
慕工程0101907
TA贡献1887条经验 获得超5个赞
我必须在iOS 8和更高版本上实现这一点,并在iPhone上进行测试。接受的答案只是呈现滚动视图的可见部分,而图像的其余部分仍然是空白的。
我试着用drawViewHierarchyInRect
-运气不好取决于afterScreenUpdates
赋存true
或false
我被拉伸了一部分图像或者只是部分内容。
实现正确的快照的唯一方法是UIScrollView
其全部内容是将其添加到另一个临时视图中,然后呈现它。
示例代码如下(scrollview
是我的VC中的插座)
func getImageOfScrollView() -> UIImage { var image = UIImage() UIGraphicsBeginImageContextWithOptions(self.scrollView.contentSize, false, UIScreen.mainScreen().scale) // save initial values let savedContentOffset = self.scrollView.contentOffset let savedFrame = self.scrollView.frame let savedBackgroundColor = self.scrollView.backgroundColor // reset offset to top left point self.scrollView.contentOffset = CGPointZero // set frame to content size self.scrollView.frame = CGRectMake(0, 0, self.scrollView.contentSize.width, self.scrollView.contentSize.height) // remove background self.scrollView.backgroundColor = UIColor.clearColor() // make temp view with scroll view content size // a workaround for issue when image on ipad was drawn incorrectly let tempView = UIView(frame: CGRectMake(0, 0, self.scrollView.contentSize.width, self.scrollView.contentSize.height)) // save superview let tempSuperView = self.scrollView.superview // remove scrollView from old superview self.scrollView.removeFromSuperview() // and add to tempView tempView.addSubview(self.scrollView) // render view // drawViewHierarchyInRect not working correctly tempView.layer.renderInContext(UIGraphicsGetCurrentContext()) // and get image image = UIGraphicsGetImageFromCurrentImageContext() // and return everything back tempView.subviews[0].removeFromSuperview() tempSuperView?.addSubview(self.scrollView) // restore saved settings self.scrollView.contentOffset = savedContentOffset self.scrollView.frame = savedFrame self.scrollView.backgroundColor = savedBackgroundColor UIGraphicsEndImageContext() return image}
慕姐8265434
TA贡献1813条经验 获得超2个赞
extension UIView { func screenshot() -> UIImage { if(self is UIScrollView) { let scrollView = self as! UIScrollView let savedContentOffset = scrollView.contentOffset let savedFrame = scrollView.frame UIGraphicsBeginImageContext(scrollView.contentSize) scrollView.contentOffset = .zero self.frame = CGRect(x: 0, y: 0, width: scrollView.contentSize.width, height: scrollView.contentSize.height) self.layer.render(in: UIGraphicsGetCurrentContext()!) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext(); scrollView.contentOffset = savedContentOffset scrollView.frame = savedFrame return image! } UIGraphicsBeginImageContext(self.bounds.size) self.layer.render(in: UIGraphicsGetCurrentContext()!) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image! }}
- 3 回答
- 0 关注
- 1442 浏览
添加回答
举报
0/150
提交
取消