How to take screenshot of UIScrollView visible area?

前端 未结 8 1639
鱼传尺愫
鱼传尺愫 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:28

    Another approach would be to use the contentOffset to adjust the layer's visible area and capture only the currently visible area of 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();
    

    Cheers :)

提交回复
热议问题