How to take screenshot of UIScrollView visible area?

前端 未结 8 1611
鱼传尺愫
鱼传尺愫 2021-02-13 16:57

How do I take a 1:1 screenshot of UIScrollView visible area? The content may be larger or smaller than UIScrollView bounds as well as half-hidden (I\'ve implemented custom scrol

8条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-13 17:29

    I've found a solution myself - I took screenshot of the whole view and then crop it to the size and position of UIScrollView frame.

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

提交回复
热议问题