TonyReet / TYSnapshotScroll

一句代码保存截图,将 UIScrollView UITableView UICollectionView UIWebView WKWebView 网页 保存 为 长图 查看。Save the scroll view page as an image,support UIScrollView,UITableView,UICollectionView,UIWebView,WKWebView.(Support iOS13)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

在scrollview上截图的时候,下面的控件会闪现一下在顶部。然后截图出来的时候消失了

CoderWeiLee opened this issue · comments

exp
可以看下这个gif,注意最上面截图的时候出现的,想问下这个问题怎么解决哇

那三个颜色的图例是在下面的。但是截图的时候,突然出现了一下在顶部

看了下实现代码,我估计是内部移动layer的frame造成的,目的是为了截取长图,拼接图片。但是我写了一个测试demo,发现内部获取context又获取不到了,按道理来说,这跟写不写在- drawRect()方法没啥关系吧。

//
//  ViewController.m
//  TestOC
//
//  Created by wei.li on 2020/7/24.
//  Copyright © 2020 wei.li. All rights reserved.
//
#import "ViewController.h"
#import "TYSnapshotScroll.h"
@interface ViewController ()
@property (nonatomic, strong) UIScrollView *scroll;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIButton *btn;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.scroll = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    self.scroll.contentSize = CGSizeMake(0, 1000);
    [self.view addSubview:self.scroll];
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 100, 100, 100)];
    [self.scroll addSubview:self.imageView];
    self.btn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.btn.frame = CGRectMake(100, 200, 88, 30);
    [self.btn setTitle:@"点击截图" forState:UIControlStateNormal];
    [self.btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    self.btn.backgroundColor = [UIColor yellowColor];
    [self.btn addTarget:self action:@selector(beginScreenShot) forControlEvents:UIControlEventTouchUpInside];
    [self.scroll addSubview:self.btn];
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(50, 800, 100, 80) byRoundingCorners:UIRectCornerTopLeft | UIRectCornerAllCorners cornerRadii:CGSizeMake(5, 5)];
    path.lineWidth = 20;
    CAShapeLayer *shape = [CAShapeLayer layer];
    shape.path = path.CGPath;
    shape.fillColor = [UIColor greenColor].CGColor;
    [self.scroll.layer addSublayer:shape];
}

- (void)beginScreenShot {
    [TYSnapshotScroll screenSnapshot:self.scroll finishBlock:^(UIImage *snapshotImage) {
        self.imageView.image = snapshotImage;
    }];
    
}


@end

想问下这种情况下,获取不到上下文要怎么破?

我发现如果界面上添加的是View控件,不会出现这样的情况。如果添加的是通过layer绘制出来的图形,会出现这样下面的layer图层跑到上面来,停顿一下消失的bug。应该是框架里的这一行代码引起的:

 onMainThreadSync(^{
        oldFrame = self.layer.frame;
        // 划到bottom
        if (self.contentSize.height > self.frame.size.height) {
            self.contentOffset = CGPointMake(0, self.contentSize.height - self.bounds.size.height + self.contentInset.bottom);
        }
        
        self.layer.frame = CGRectMake(0, 0, self.contentSize.width, self.contentSize.height);
    });

原因找到了,是那个delay延时引起的

看了下实现代码,我估计是内部移动layer的frame造成的,目的是为了截取长图,拼接图片。但是我写了一个测试demo,发现内部获取context又获取不到了,按道理来说,这跟写不写在- drawRect()方法没啥关系吧。

//
//  ViewController.m
//  TestOC
//
//  Created by wei.li on 2020/7/24.
//  Copyright © 2020 wei.li. All rights reserved.
//
#import "ViewController.h"
#import "TYSnapshotScroll.h"
@interface ViewController ()
@property (nonatomic, strong) UIScrollView *scroll;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIButton *btn;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.scroll = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    self.scroll.contentSize = CGSizeMake(0, 1000);
    [self.view addSubview:self.scroll];
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 100, 100, 100)];
    [self.scroll addSubview:self.imageView];
    self.btn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.btn.frame = CGRectMake(100, 200, 88, 30);
    [self.btn setTitle:@"点击截图" forState:UIControlStateNormal];
    [self.btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    self.btn.backgroundColor = [UIColor yellowColor];
    [self.btn addTarget:self action:@selector(beginScreenShot) forControlEvents:UIControlEventTouchUpInside];
    [self.scroll addSubview:self.btn];
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(50, 800, 100, 80) byRoundingCorners:UIRectCornerTopLeft | UIRectCornerAllCorners cornerRadii:CGSizeMake(5, 5)];
    path.lineWidth = 20;
    CAShapeLayer *shape = [CAShapeLayer layer];
    shape.path = path.CGPath;
    shape.fillColor = [UIColor greenColor].CGColor;
    [self.scroll.layer addSublayer:shape];
}

- (void)beginScreenShot {
    [TYSnapshotScroll screenSnapshot:self.scroll finishBlock:^(UIImage *snapshotImage) {
        self.imageView.image = snapshotImage;
    }];
    
}


@end

想问下这种情况下,获取不到上下文要怎么破?

看了下实现代码,我估计是内部移动layer的frame造成的,目的是为了截取长图,拼接图片。但是我写了一个测试demo,发现内部获取context又获取不到了,按道理来说,这跟写不写在- drawRect()方法没啥关系吧。

//
//  ViewController.m
//  TestOC
//
//  Created by wei.li on 2020/7/24.
//  Copyright © 2020 wei.li. All rights reserved.
//
#import "ViewController.h"
#import "TYSnapshotScroll.h"
@interface ViewController ()
@property (nonatomic, strong) UIScrollView *scroll;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIButton *btn;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.scroll = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    self.scroll.contentSize = CGSizeMake(0, 1000);
    [self.view addSubview:self.scroll];
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 100, 100, 100)];
    [self.scroll addSubview:self.imageView];
    self.btn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.btn.frame = CGRectMake(100, 200, 88, 30);
    [self.btn setTitle:@"点击截图" forState:UIControlStateNormal];
    [self.btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    self.btn.backgroundColor = [UIColor yellowColor];
    [self.btn addTarget:self action:@selector(beginScreenShot) forControlEvents:UIControlEventTouchUpInside];
    [self.scroll addSubview:self.btn];
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(50, 800, 100, 80) byRoundingCorners:UIRectCornerTopLeft | UIRectCornerAllCorners cornerRadii:CGSizeMake(5, 5)];
    path.lineWidth = 20;
    CAShapeLayer *shape = [CAShapeLayer layer];
    shape.path = path.CGPath;
    shape.fillColor = [UIColor greenColor].CGColor;
    [self.scroll.layer addSublayer:shape];
}

- (void)beginScreenShot {
    [TYSnapshotScroll screenSnapshot:self.scroll finishBlock:^(UIImage *snapshotImage) {
        self.imageView.image = snapshotImage;
    }];
    
}


@end

想问下这种情况下,获取不到上下文要怎么破?

谢谢提供的demo,demo里面的context获取不到主要是因为"self.scroll.contentSize = CGSizeMake(0, 1000);",这里的宽度为0,现在处理为截取之前判断contentSize的宽度,如果为0,则取scrollView的宽度,完成以后还原。另gif图的问题,确实看不清楚,如果有高清图片,烦请发送到邮箱ktonyreet@gmail.com,谢谢反馈。

commented

借楼问下 这个bug解决了吗