iOS 截屏拼图

作者: 行业碧油鸡 | 来源:发表于2018-04-13 15:47 被阅读11次

+++
Categories = ["iOS",]
Tags = ["iOS","snapshot",]
date = "2014-12-22T11:22:30+08:00"
title = "iOS 截屏拼图"

+++

1. 普通界面

ios 7 以后截图

ios7中添加了调用 snapshotViewAfterScreenUpdates 创建一个复合视图的快照。然后返回一个uiview对象来表示调用视图的整体外观。

Supplying YES for -snapshotViewAfterScreenUpdates: means it needs a trip back to the runloop to actually draw the image. If you supply NO, it will try immediately, but if your view is off screen or otherwise hasn't yet drawn to the screen, the snapshot will be empty.

因为返回的是一个view对象,所以,你可以更改它以及它的layer属性.但是呢,你不能够修改它的layer的content属性;如果你试图这么做,将不会有任何效果.如果当前的view还没有渲染,或者这么说吧,因为还没有出现在屏幕上,那么,这个截取的view将不会有能显示的content.

如果你想要加载一个图形效果,比如blur,请使用这个方法 drawViewHierarchyInRect:afterScreenUpdates: 来代替.

#import "UIImage+ImageEffects.h"

- (void)createBlurredSnapshot {
    
    UIGraphicsBeginImageContextWithOptions(self.targetImageView.bounds.size, NO, 0);
    
    BOOL result = [self.sourceImageView drawViewHierarchyInRect:self.targetImageView.bounds afterScreenUpdates:YES];
    
    UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
   if (result)
        self.targetImageView.image = [snapshotImage applyLightEffect];
    else
        NSLog(@"drawViewHierarchyInRect failed");
}
iOS7 以前截图
- (UIImage *)captureCurrentView:(UIView *)view {
    CGRect frame = view.frame;
    UIGraphicsBeginImageContext(frame.size);
    CGContextRef contextRef = UIGraphicsGetCurrentContext();
    [view.layer renderInContext:contextRef];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
    
    // 获取指定区域的截图
    CGImageRef imageRef = image.CGImage;
    CGRect rect = CGRectMake(0, 0, 300, 300);  //设置指定区域
    CGImageRef editImageRef = CGImageCreateWithImageInRect(imageRef, rect);
    UIImage *editImage = [UIImage alloc] initWithCGImage:editImageRef];
    return editImage;
}

2. UIScorllView 截图

- (void)screenShot{  
        UIImage *image = nil;         
        UIGraphicsBeginImageContext(_scrollView.contentSize);  
      
        {  
            CGPoint savedContentOffset = _scrollView.contentOffset;  
            CGRect savedFrame = _scrollView.frame;  
            _scrollView.contentOffset = CGPointZero;  
      
            _scrollView.frame = CGRectMake(0, 0, m_scrollView.contentSize.width, m_scrollView.contentSize.height);        
            [_scrollView.layer renderInContext: UIGraphicsGetCurrentContext()];  
      
            image = UIGraphicsGetImageFromCurrentImageContext();       
      
            _scrollView.contentOffset = savedContentOffset;  
            _scrollView.frame = savedFrame;  
        }  
        UIGraphicsEndImageContext();      
      
        if (image != nil) {  
            NSLog(@"success snapshot!");  
        }  
    }  

3. 截图后拼图

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

相关文章

  • iOS 截屏拼图

    +++Categories = ["iOS",]Tags = ["iOS","snapshot",]date = ...

  • 截屏拼图

    刚才看别人博客关于截屏拼图的,自己觉得挺有意思的,于是自己动手也写了一个简单demo,一般我的视图的内容比较多,超...

  • flutter:截屏

    1.flutter-截屏组件 2.flutter-截屏插件 3.flutter-iOS原生截屏 iOS代码 4.获...

  • (最新)iOS截屏

    ios webview 截屏:ios截屏 前言:介绍一下截屏有很多种做法1:截当前屏幕内容2:截整个视图的所有内容...

  • ios截屏

    ios截屏

  • 你知道用户下一步想要做什么吗?

    当使用APP内的截屏按钮连续截屏2次时,就会出现“点击分享+一键拼图”的提示框。点击“一键拼图”即跳转至下图,用户...

  • iOS 应用内截屏分享

    需求:捕获用户截屏操作,并建议用户截屏后的操作。虽然iOS11 有系统的截屏,但 APP 内截屏可便捷操作。 封装...

  • iOS 截屏&长截屏

    截屏在 iOS 开发中经常用到,本篇文章讲的是监听用户截屏操作,并且获取截屏图片,如果当前是UIScrollVie...

  • iOS屏幕截图功能

    iOS7.0之前的系统,可以通过以下代码实现截屏功能。 iOS7.0之后,系统中封装了截屏的方法- (UIView...

  • iOS截屏

    1. 一句代码截屏 2. UIGraphics 3. 还有就是之前一个大佬写的给webview截长图的 其实就是利...

网友评论

    本文标题:iOS 截屏拼图

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