iOS - Quartz 2D 手势截屏绘制

简介: 1、绘制手势截屏具体实现代码见 GitHub 源码 QExtensionQTouchClipView.h @interface QTouchClipView : UIView /** * 创建手势截屏视图控件,获取截屏结果 * * @par...

1、绘制手势截屏

  • 具体实现代码见 GitHub 源码 QExtension

  • QTouchClipView.h

        @interface QTouchClipView : UIView
    
        /**
         *  创建手势截屏视图控件,获取截屏结果
         *
         *  @param view     截取图片的视图控件
         *  @param result   手势截屏结果
         *
         *  @return 手势截屏视图控件
         */
        + (instancetype)q_touchClipViewWithView:(UIView *)view
                                     clipResult:(void (^)(UIImage * _Nullable image))result;
    
        @end
  • QTouchClipView.m

        @interface QTouchClipView ()
    
        /// 截取图片的视图控件
        @property (nonatomic, strong) UIView *baseView;
    
        /// 滑动手势结果
        @property (nonatomic, copy) void (^resultBlock)(UIImage * _Nullable);
    
        /// 触摸开始结束点
        @property (nonatomic, assign) CGPoint startP;
        @property (nonatomic, assign) CGPoint endP;
    
        @end
    
        @implementation QTouchClipView
    
        /// 创建手势截屏视图控件,获取截屏结果
        + (instancetype)q_touchClipViewWithView:(UIView *)baseView
                                     clipResult:(void (^)(UIImage * _Nullable image))result {
    
            QTouchClipView *clipView = [[self alloc] initWithFrame:baseView.frame];
    
            clipView.baseView = baseView;
            clipView.resultBlock = result;
    
            return clipView;
        }
    
        /// 初始化
        - (instancetype)initWithFrame:(CGRect)frame {
    
            if (self = [super initWithFrame:frame]) {
                self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
            }
            return self;
        }
    
        /// 触摸开始
        - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event {
    
            // 获取触摸起始点位置
            CGPoint startPoint = [touches.anyObject locationInView:self];
            self.startP = startPoint;
        }
    
        /// 触摸移动
        - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event {
    
            // 获取触摸点位置
            CGPoint touchPoint = [touches.anyObject locationInView:self];
            self.endP = touchPoint;
    
            // 刷新视图
            [self setNeedsDisplay];
        }
    
        /// 触摸结束
        - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event {
    
            // 截取屏幕图片
            UIGraphicsBeginImageContextWithOptions(self.baseView.bounds.size, NO, 0);
    
            CGContextRef ctx = UIGraphicsGetCurrentContext();
            [self.baseView.layer renderInContext:ctx];
    
            UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
            UIGraphicsEndImageContext();
    
            // 切割图片
            CGFloat x = self.startP.x;
            CGFloat y = self.startP.y;
            CGFloat w = self.endP.x - x;
            CGFloat h = self.endP.y - y;
    
            CGRect cutRect = CGRectMake(x * 2, y * 2, w * 2, h * 2);
    
            CGImageRef cgImage = CGImageCreateWithImageInRect(image.CGImage, cutRect);
            UIImage *newImage = [[UIImage alloc] initWithCGImage:cgImage];
            CGImageRelease(cgImage);
    
            // 返回截取结果
            if (self.resultBlock) {
                self.resultBlock(newImage);
            }
    
            // 移除截取视图控件
            [self removeFromSuperview];
            self.startP = CGPointZero;
            self.endP = CGPointZero;
    
            // 刷新视图
            [self setNeedsDisplay];
        }
    
        /// 触摸取消
        - (void)touchesCancelled:(NSSet *)touches withEvent:(nullable UIEvent *)event {
            [self touchesEnded:touches withEvent:event];
        }
    
        /// 绘制触摸区域
        - (void)drawRect:(CGRect)rect {
    
            CGFloat x = self.startP.x;
            CGFloat y = self.startP.y;
            CGFloat w = self.endP.x - x;
            CGFloat h = self.endP.y - y;
    
            CGRect clipRect = CGRectMake(x, y, w, h);
    
            UIBezierPath *path = [UIBezierPath bezierPathWithRect:clipRect];
            [[[UIColor whiteColor] colorWithAlphaComponent:0.2] setFill];
            [path fill];
        }
    
        @end
  • ViewController.m

        // 创建手势截屏视图
        QTouchClipView *touchClipView = [QTouchClipView q_touchClipViewWithView:self.imageView
                                                                     clipResult:^(UIImage * _Nullable image) {
    
            // 获取处理截屏结果
            if (image) {
                UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
            }
        }];
    
        // 添加手势截屏视图
        [self.view addSubview:touchClipView];
  • 效果

    Quartz2D100Quartz2D101

目录
相关文章
|
5月前
|
iOS开发 UED 开发者
iOS 手势中cancelsTouchesInView delaysTouchesBegan delaysTouchesEnded 三种属性的使用
iOS 手势中cancelsTouchesInView delaysTouchesBegan delaysTouchesEnded 三种属性的使用
198 9
|
9月前
|
iOS开发
你知道IOS移动端到操作手势有哪些吗?
你知道IOS移动端到操作手势有哪些吗?
197 0
|
9月前
|
XML Java Android开发
Android App开发音量调节中实现拖动条和滑动条和音频管理器AudioManager讲解及实战(超详细 附源码和演示视频)
Android App开发音量调节中实现拖动条和滑动条和音频管理器AudioManager讲解及实战(超详细 附源码和演示视频)
328 0
|
iOS开发
(六)IOS手势和触摸的用法
(六)IOS手势和触摸的用法
246 0
|
程序员 API iOS开发
iOS UIView添加快捷手势回调
iOS UIView添加快捷手势回调
|
iOS开发
如何使用iOS手势UIGestureRecognizer
如何使用iOS手势UIGestureRecognizer
124 0
|
iOS开发
(七) IOS 响应者链和手势
(七) IOS 响应者链和手势
381 0
|
Android开发
安卓显示视频画面的动画效果及代码
最近客户提出,视频画面显示时黑屏体验不好,要求显示背景图。等视频画面来了再显示。 这个问题是怎么引起的?就在于安卓的View,设置了背景之后,就无法再显示视频画面(实际上肯定有绘制视频画面,UI流程上使得背景图一直显示)。
141 0

热门文章

最新文章