获取 UIScrollView 的屏幕截图,包括屏幕外部分 [英] Getting a screenshot of a UIScrollView, including offscreen parts

查看:41
本文介绍了获取 UIScrollView 的屏幕截图,包括屏幕外部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIScrollView 后代,它实现了如下所示的 takeScreenshot 方法:

-(void)takeScreenshot {CGRect contextRect = CGRectMake(0, 0, 768, 1004);UIGraphicsBeginImageContext(contextRect.size);[self.layer renderInContext:UIGraphicsGetCurrentContext()];UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();//对这里的 viewImage 做一些事情.}

这基本上移动到滚动视图的顶部,并对可见区域进行截图.当 iPad 面向纵向时它工作正常,但当它处于横向时,图像的底部被切断(因为可见区域的高度仅为 748,而不是 1004).

是否可以获取 UIScrollView 的快照,包括屏幕上未显示的区域?还是我需要向下滚动视图,拍第二张照片并将它们拼接在一起?

解决方案

这是有效的代码......

- (IBAction) renderScrollViewToImage{UIImage* image = nil;UIGraphicsBeginImageContext(_scrollView.contentSize);{CGPoint savedContentOffset = _scrollView.contentOffset;CGRectsavedFrame = _scrollView.frame;_scrollView.contentOffset = CGPointZero;_scrollView.frame = CGRectMake(0, 0, _scrollView.contentSize.width, _scrollView.contentSize.height);[_scrollView.layer renderInContext: UIGraphicsGetCurrentContext()];image = UIGraphicsGetImageFromCurrentImageContext();_scrollView.contentOffset = savedContentOffset;_scrollView.frame = 保存帧;}UIGraphicsEndImageContext();如果(图像!= nil){[UIImagePNGRepresentation(image) writeToFile: @"/tmp/test.png" 原子:YES];system("打开/tmp/test.png");}}

最后几行只是将图像写入/tmp/test.png,然后在 Preview.app 中打开它.这显然只适用于模拟器:-)

ScrollViewScreenShot Github 存储库中的完整项目

I have a UIScrollView decendent that implements a takeScreenshot method that looks like this:

-(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.
}

This basically moves to the top of the scroll view, and takes a screenshot of the visible area. It works fine when the iPad is oriented portrait, but when it's in landscape the bottom of the image is cut off (as the height of the visible area is only 748, not 1004).

Is it possible to get a snapshot of the UIScrollView, including areas not on screen? Or do I need to scroll the view down, take a second photo and stitch them together?

解决方案

Here is code that works ...

- (IBAction) renderScrollViewToImage
{
    UIImage* image = nil;

    UIGraphicsBeginImageContext(_scrollView.contentSize);
    {
        CGPoint savedContentOffset = _scrollView.contentOffset;
        CGRect savedFrame = _scrollView.frame;

        _scrollView.contentOffset = CGPointZero;
        _scrollView.frame = CGRectMake(0, 0, _scrollView.contentSize.width, _scrollView.contentSize.height);

        [_scrollView.layer renderInContext: UIGraphicsGetCurrentContext()];     
        image = UIGraphicsGetImageFromCurrentImageContext();

        _scrollView.contentOffset = savedContentOffset;
        _scrollView.frame = savedFrame;
    }
    UIGraphicsEndImageContext();

    if (image != nil) {
        [UIImagePNGRepresentation(image) writeToFile: @"/tmp/test.png" atomically: YES];
        system("open /tmp/test.png");
    }
}

The last few lines simply write the image to /tmp/test.png and then opens it in Preview.app. This obviously only works on in the Simulator :-)

Complete project in the ScrollViewScreenShot Github Repository

这篇关于获取 UIScrollView 的屏幕截图,包括屏幕外部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆