锁定区域截图

作者: shushuzhen | 来源:发表于2017-05-24 09:26 被阅读59次

最近对接别人家的摄像头,说真的,人家的SDK,你只需要知道功能在哪,
然后拿过来对接一下你自己的视图,毕竟人家写的那个代码,我是真的看不懂。
主要是还有一些你需要的功能他并没有,比如下面说的一个在规定区域内截图功能。
我这个需求是为了截下摄像头的界面,摄像头界面是用一个imageView视图来加载的:
下面是一个效果图,展示了点击一个view之后将另外一个imageview的颜色换了,
顺便将此view截图保存到相册中。

截图Demo.gif

代码如下:

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) UIImageView *temp;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];


    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 200, 100, 100)];
    [self.view addSubview:image];
    image.backgroundColor = [UIColor grayColor];
    _temp = image;
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(100, 300, 100, 100)];
    [self.view addSubview:view];
    view.backgroundColor = [UIColor redColor];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(capture:)];
    [view addGestureRecognizer:tap];
    
}
- (void )capture:(UITapGestureRecognizer *)tap{
    // 1.开启上下文
    UIGraphicsBeginImageContextWithOptions(tap.view.frame.size, NO, 0.0);
    
    // 2.将控制器view的layer渲染到上下文
    [tap.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    
    // 3.取出图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    // 4.结束上下文
    UIGraphicsEndImageContext();
   
    _temp.image = newImage;
    
    UIImageWriteToSavedPhotosAlbum(newImage, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);
    
}
- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    NSString *message = @"保存失败";
    if (!error) {
        message = @"成功保存到相册";
    }else
    {
        message = [error description];
    }
    NSLog(@"message is %@",message);
}

相关文章

网友评论

    本文标题:锁定区域截图

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