zoukankan      html  css  js  c++  java
  • iOS中手指拖动生成截图范围并截图

    #import "ViewController.h"

    @interface ViewController ()

    @property (nonatomic, assign) CGPoint startP;

    @property (weak, nonatomic) IBOutlet UIImageView *imageV;

    @property (nonatomic, weak) UIView *clipView;

    @end

    @implementation ViewController

    - (UIView *)clipView{

    if (_clipView == nil) {

    UIView *view = [[UIView alloc] init];

    _clipView = view;

    view.backgroundColor = [UIColor blackColor];

    view.alpha = 0.5;

    [self.view addSubview:view];

    }

    return _clipView;

    }

    - (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    // 给控制器的view添加一个pan手势

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];

    [self.view addGestureRecognizer:pan];

    }

    - (void)pan:(UIPanGestureRecognizer *)pan

    {

    CGPoint endA = CGPointZero;

    if (pan.state == UIGestureRecognizerStateBegan) {

     // 一开始拖动的时候

    // 获取一开始触摸点

    _startP = [pan locationInView:self.view];

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

    // 一直拖动

    // 获取结束点

    endA = [pan locationInView:self.view];

    CGFloat w = endA.x - _startP.x;

    CGFloat h = endA.y - _startP.y;

    // 获取截取范围

    CGRect clipRect = CGRectMake(_startP.x, _startP.y, w, h);

    // 生成截屏的view

    self.clipView.frame = clipRect;

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

    // 图片裁剪,生成一张新的图片

    // 开启上下文

    // 如果不透明,默认超出裁剪区域会变成黑色,通常都是透明

    UIGraphicsBeginImageContextWithOptions(_imageV.bounds.size, NO, 0);

    // 设置裁剪区域

    UIBezierPath *path =  [UIBezierPath bezierPathWithRect:_clipView.frame];

    [path addClip];

    // 获取上下文

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    // 把控件上的内容渲染到上下文

    [_imageV.layer renderInContext:ctx];

    // 生成一张新的图片

    _imageV.image = UIGraphicsGetImageFromCurrentImageContext();

    // 关闭上下文

    UIGraphicsEndImageContext();

    // 先移除

    [_clipView removeFromSuperview];

    // 截取的view设置为nil

    _clipView = nil;

    }

  • 相关阅读:
    Jmeter接口测试时传递json格式的数据
    selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element 定位frame中的元素
    selenium报错“selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.”的解决方案
    python+selenium如何定位页面的元素,有几种定位元素的方法?
    c#中的表达式
    占位符的使用
    数据类型(变量的声明与赋值)
    Hello World 老调重谈
    易语言转C#小试牛刀
    开博了
  • 原文地址:https://www.cnblogs.com/CJH5209/p/6032718.html
Copyright © 2011-2022 走看看