截屏拼图

刚才看别人博客关于截屏拼图的,自己觉得挺有意思的,于是自己动手也写了一个简单demo,一般我的视图的内容比较多,超过了屏幕,比如UITableView控件,这是可以通过修改contentOffset的属性截图,截取几张部分图片然后拼成一张大图,不说了,先看看代码。
简单的创建一下UI

-(void)setupUI
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    _tableView = tableView;
    
    UIBarButtonItem *rightBarBtn = [[UIBarButtonItem alloc] initWithTitle:@"截图" style:UIBarButtonItemStylePlain target:self action:@selector(screenShot:)];
    self.navigationItem.rightBarButtonItem = rightBarBtn;
    
    UIBarButtonItem *leftBarBtn = [[UIBarButtonItem alloc] initWithTitle:@"拼图" style:UIBarButtonItemStylePlain target:self action:@selector(combinImg:)];
    self.navigationItem.leftBarButtonItem = leftBarBtn;
}

UI效果图:


截屏拼图_第1张图片
屏幕快照 2016-01-08 上午11.31.49.png

看看截图代码:

-(void)screenShot:(id)sender
{
   
    CGFloat contentHight = self.tableView.contentInset.top + self.tableView.contentSize.height;
    CGFloat imgHight = self.tableView.frame.size.height;
    //上取整,有个取整函数,忘了,这取整也可以精度0.00001了
    NSInteger imgCount =(NSInteger)((contentHight / imgHight) + 0.99999);
    NSString *rootPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    [self.imgArr removeAllObjects];    
    for (int i = 0; i 8.0) {
            [self.tableView drawViewHierarchyInRect:CGRectMake(0, 0, self.view.bounds.size.width, imgHight) afterScreenUpdates:YES];
        }
        else
        {
            [self.tableView.layer renderInContext:context];
        }
        UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
        [self.imgArr addObject:img];
        UIGraphicsEndImageContext();
        NSData *imgData = UIImagePNGRepresentation(img);
        NSString* path = [rootPath stringByAppendingPathComponent:[NSString stringWithFormat:@"img%d.png",i]];    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            //异步写到文件中可打开文件查看
            [imgData writeToFile:path atomically:YES];
        });
        NSLog(@"path:%@",path);
        self.tableView.contentOffset = CGPointMake(0,self.tableView.contentOffset.y + self.tableView.frame.size.height);
    }
}

将截图的图片保存到imgArr数组中,再来看看拼图代码:

-(void)combinImg:(id)sender
{
    if (self.imgArr.count <=0 ) {
        return;
    }
    NSString *rootPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
 CGFloat tatolHight = self.tableView.contentSize.height +  self.tableView.contentInset.top;
UIGraphicsBeginImageContextWithOptions(CGSizeMake(self.tableView.frame.size.width, tatolHight), NO, 1);    
    CGFloat orgy = 0;
    for(int i = 0; i < self.imgArr.count;i++)
    {
        UIImage *image = (UIImage*)self.imgArr[i];
        [image drawInRect:CGRectMake(0, orgy,image.size.width, image.size.height)];
        orgy += image.size.height;
    }
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.tableView.frame.size.height )];
    scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, self.tableView.contentSize.height + 64);
    scrollView.backgroundColor = [UIColor brownColor];
    [self.view addSubview:scrollView];
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, scrollView.frame.size.width, self.tableView.contentSize.height)];
    imgView.backgroundColor = [UIColor purpleColor];
    imgView.image = img;
    [scrollView addSubview:imgView];
    //写到文件中可打开文件查看
    NSString *path = [rootPath stringByAppendingPathComponent:@"combin.png"];
    NSData *imgData = UIImagePNGRepresentation(img);
    [imgData writeToFile:path atomically:YES];
}

拼图的图片如下:

截屏拼图_第2张图片
combin.png

运行结果,很显然完美拼接!
代码上传github: https://github.com/jiangtaidi/ShotPinTu.git

你可能感兴趣的:(截屏拼图)