美文网首页iOS开发那些事iOS-SwiftSwiftUI
iOS-全屏截图、view生成图片、scrollView生成长图

iOS-全屏截图、view生成图片、scrollView生成长图

作者: zwing | 来源:发表于2022-07-12 15:25 被阅读0次

    1. 全屏截图

    + (UIImage *)snapshotScreen {
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        UIGraphicsBeginImageContextWithOptions(window.bounds.size, NO, [UIScreen mainScreen].scale);
        [window.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    }
    

    2. view生成图片

    + (UIImage *)snapshotWithView:(UIView *)view {
        UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, [UIScreen mainScreen].scale);
        [view.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    }
    

    2. scrollView生成长图

    + (UIImage *)snapshotWithScrollView:(UIScrollView *)scrollView {
        CGPoint savedContentOffset = scrollView.contentOffset;
        CGRect savedFrame = scrollView.frame;
        
        scrollView.contentOffset = CGPointZero;
        scrollView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height);
    
        UIGraphicsBeginImageContextWithOptions(scrollView.contentSize, NO, [UIScreen mainScreen].scale);
        [scrollView.layer renderInContext: UIGraphicsGetCurrentContext()];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        scrollView.contentOffset = savedContentOffset;
        scrollView.frame = savedFrame;
        
        return image;
    }
    

    相关文章

      网友评论

        本文标题:iOS-全屏截图、view生成图片、scrollView生成长图

        本文链接:https://www.haomeiwen.com/subject/lruvbrtx.html