13

如何对 UIScrollView 可见区域进行 1:1 截图?内容可能大于或小于 UIScrollView 边界以及半隐藏(我已经为较小的内容实现了自定义滚动,所以它不在左上角)。我在模拟器上达到了预期的效果,但在设备本身上却没有:

-(UIImage *)imageFromCombinedContext:(UIView *)background {
      UIImage *image;
      CGRect vis = background.bounds;
      CGSize size = vis.size;
      UIGraphicsBeginImageContext(size);
      [background.layer affineTransform];
      [background.layer renderInontext:UIGraphicsGetCurrentContext()];
      image = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
      CGImageRef imref = CGImageCreateWithImageInRect([image CGImage], vis);
      image = [UIImage imageWithCGImage:imref];
      CGImageRelease(imref);
      return image;
}
4

8 回答 8

32

另一种方法是使用contentOffset调整图层的可见区域并捕获的当前可见区域UIScrollView

UIScrollView *contentScrollView;....//scrollview instance

UIGraphicsBeginImageContextWithOptions(contentScrollView.bounds.size, 
                                       YES, 
                                       [UIScreen mainScreen].scale);

//this is the key
CGPoint offset=contentScrollView.contentOffset;
CGContextTranslateCTM(UIGraphicsGetCurrentContext(), -offset.x, -offset.y); 

[contentScrollView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *visibleScrollViewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

干杯:)

于 2013-07-23T20:30:16.697 回答
6

Swift 版本的 Abduliam Rehmanius 回答。

func screenshot() -> UIImage {

        UIGraphicsBeginImageContextWithOptions(self.scrollCrop.bounds.size, true, UIScreen.mainScreen().scale);
        //this is the key
        let offset:CGPoint = self.scrollCrop.contentOffset;
        CGContextTranslateCTM(UIGraphicsGetCurrentContext(), -offset.x, -offset.y);
        self.scrollCrop.layer.renderInContext(UIGraphicsGetCurrentContext()!);
        let visibleScrollViewImage: UIImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return visibleScrollViewImage;
    }

斯威夫特 4 版本:

func screenshot() -> UIImage {
UIGraphicsBeginImageContextWithOptions(self.scrollCrop.bounds.size, false, UIScreen.main.scale)
        let offset = self.scrollCrop.contentOffset
        let thisContext = UIGraphicsGetCurrentContext()
        thisContext?.translateBy(x: -offset.x, y: -offset.y)
        self.scrollCrop.layer.render(in: thisContext!)
        let visibleScrollViewImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
return visibleScrollViewImage
}
于 2016-03-26T08:33:37.863 回答
4

我自己找到了一个解决方案 - 我截取了整个视图,然后将其裁剪为UIScrollView框架的大小和位置。

-(UIImage *)imageFromCombinedContext:(UIView *)background 
{
      UIImage *image;
      CGSize size = self.view.frame.size;
      UIGraphicsBeginImageContext(size);
      [background.layer affineTransform];
      [self.view.layer.layer renderInContext:UIGraphicsGetCurrentContext()];
      image = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
      CGImageRef imgRef = CGImageCreateWithImageInRect([image CGImage],background.frame);
      image = [UIImage imageWithCGImage:imref];
      CGImageRelease(imref);
      return image;
}
于 2012-06-11T09:23:57.197 回答
4

Swift 4 版本的 Abduliam Rehmanius 回答UIScrollView extension与翻译一样,没有慢速裁剪

extension UIScrollView {

    var snapshotVisibleArea: UIImage? {
        UIGraphicsBeginImageContext(bounds.size)
        UIGraphicsGetCurrentContext()?.translateBy(x: -contentOffset.x, y: -contentOffset.y)
        layer.render(in: UIGraphicsGetCurrentContext()!)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
}
于 2018-03-29T12:00:52.687 回答
3

杰弗里孙有正确的答案。只需将您的滚动视图放在另一个视图中。获取容器视图以在上下文中呈现。完毕。

在下面的代码中,cropView 包含要捕获的滚动视图。解决方案真的就是这么简单。

当我理解这个问题以及为什么我找到这个页面时,不需要滚动视图的全部内容 - 只是可见部分。

func captureCrop() -> UIImage {
    UIGraphicsBeginImageContextWithOptions(self.cropView.frame.size, true, 0.0)
    self.cropView.layer.renderInContext(UIGraphicsGetCurrentContext())
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image;
}
于 2015-03-09T21:53:56.367 回答
0

@Abduliam Rehmanius 的答案表现不佳,因为如果UIScrollView包含较大的内容区域,我们将绘制整个内容区域(即使在可见范围之外)。@Concuror 的答案的问题是它还会绘制UIScrollView.

我的解决方案是使用相同的边界UIScrollView放置一个UIView被调用的内部containerView,然后渲染containerView

containerView.renderInContext(context)
于 2014-10-18T17:39:41.383 回答
0

斯威夫特 3.0:

 func captureScreen() -> UIImage? {
    UIGraphicsBeginImageContextWithOptions(self.yourScrollViewName.bounds.size, true, UIScreen.main.scale)
    let offset:CGPoint = self.yourScrollViewName.contentOffset;
    UIGraphicsGetCurrentContext()!.translateBy(x: -offset.x, y: -offset.y);
    self.yourScrollViewName.layer.render(in: UIGraphicsGetCurrentContext()!)
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image
}

并将其用作:让 Image = captureScreen()

于 2017-04-05T13:27:32.663 回答
0

在@Concuror 代码上更新 swift 3+、4

func getImage(fromCombinedContext background: UIView) -> UIImage {
        var image: UIImage?
        let size: CGSize = view.frame.size
        UIGraphicsBeginImageContext(size)
        background.layer.affineTransform()
        view.layer.render(in: UIGraphicsGetCurrentContext()!)
        image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        let imgRef = image?.cgImage?.cropping(to: background.frame)
        image = UIImage(cgImage: imgRef!)
        // CGImageRelease(imgRef!) // Removing on Swift - 'CGImageRelease' is unavailable: Core Foundation objects are automatically memory managed
        return image ?? UIImage()
    }
于 2017-11-14T12:32:14.877 回答