iOS选定区域截图

#####选定区域截图 #####1.先来看看步骤:

  • 第一:确定选定的区域
  • 第二:添加截图的区域
  • 第三:就是图片上下文的常的见的操作

#####2.确定选定的区域

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@property (nonatomic,assign) CGPoint startP;

@property (nonatomic,weak) UIView *clipView;

@end
#pragma mark - setPan
//注意一点,uiimageview默认是不响应事件的,找错误花了不少时间
-(void) setPan{
    UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
    [self.imageView addGestureRecognizer:pan];
}
-(void) pan:(UIPanGestureRecognizer *) pan{
    CGPoint endP=CGPointZero;
    CGRect clipRect;
    if (pan.state==UIGestureRecognizerStateBegan) {
        _startP=[pan locationInView:_imageView];

    }else if (pan.state==UIGestureRecognizerStateChanged){
        endP=[pan locationInView:_imageView];
        clipRect=CGRectMake(_startP.x, _startP.y, endP.x-_startP.x, endP.y-_startP.y);
        //使用懒加载
//        UIView *view=[[UIView alloc]init];
//        view.backgroundColor=[UIColor blackColor];
//        view.alpha=0.4;
//        _clipView=view;
//        _clipView.frame=clipRect;//不能调用get方法
          self.clipView.frame=clipRect;
//        [self.view addSubview:_clipView];


    }else if (pan.state==UIGestureRecognizerStateEnded){

    }
}

#####3.添加截图的区域

#pragma mark - setPan
//注意一点,uiimageview默认是不响应事件的,找错误花了不少时间
-(void) setPan{
    UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
    [self.imageView addGestureRecognizer:pan];
}
-(void) pan:(UIPanGestureRecognizer *) pan{

    }else if (pan.state==UIGestureRecognizerStateChanged){


    }else if (pan.state==UIGestureRecognizerStateEnded){
        //添加截切
//        UIBezierPath *path=[UIBezierPath bezierPathWithRect:clipRect];//用这个只会得到空白
        NSLog(@"%@",NSStringFromCGRect(clipRect));
        NSLog(@"%@",NSStringFromCGRect(_clipView.frame));
        UIBezierPath *path=[UIBezierPath bezierPathWithRect:_clipView.frame];
        [path addClip];

    }
}

#####4.就是图片上下文的常的见的操作

-(void) pan:(UIPanGestureRecognizer *) pan{
    CGPoint endP=CGPointZero;
    CGRect clipRect;
    if (pan.state==UIGestureRecognizerStateBegan) {

    }else if (pan.state==UIGestureRecognizerStateChanged){


    }else if (pan.state==UIGestureRecognizerStateEnded){

        //获得图片上下文
        UIGraphicsBeginImageContextWithOptions(_imageView.bounds.size, NO, 0);
        NSLog(@"%@",NSStringFromCGRect(clipRect));
        NSLog(@"%@",NSStringFromCGRect(_clipView.frame));
        UIBezierPath *path=[UIBezierPath bezierPathWithRect:_clipView.frame];
        [path addClip];
        //获得图片上下文
        CGContextRef context=UIGraphicsGetCurrentContext();
//        //将整个屏幕添加到上下文上
       [self.view.layer renderInContext:context];
        //从上下文中获得图片
        UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
        //关闭图片上下文
        UIGraphicsEndImageContext();
        //写到指定的文件夹中
        NSData *data=UIImageJPEGRepresentation(image, 1);
        [data writeToFile:@"/Users/hyl/Desktop/素材/view.jpg" atomically:YES];
        //取消鼠标拉得矩形
        [_clipView removeFromSuperview];
        _clipView=nil;
    }
}

#####5.源代码地址

转载于:https://my.oschina.net/HYLApple/blog/701671

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值