美文网首页
AVCaptureVideoPreviewLayer 相机截屏

AVCaptureVideoPreviewLayer 相机截屏

作者: Maggie的小蜗居 | 来源:发表于2021-07-06 18:54 被阅读0次

    很多文章都提到是普通view或者layer的截屏方法,就不再重复

    针对特殊的 AVCaptureVideoPreviewLayer写的相机截屏如下

    先从视频流获取图片,绘制成图片,再将view绘制上去,这其中view的不能设置背景色,否则相机部分是view的底色

    ```

    -(void)cwCameraCaptureOutputSampleBuffer: (CMSampleBufferRef _Nullable)sampleBuffer bufferType: (NSInteger)bufferType{

        [self stopTimer:_cameraTimer];

        if(isStopLivess == NO){

            UIImage*image = [UIImagecw_getImageFromBGRA:sampleBufferbufferType:bufferType];

            dispatch_async(dispatch_get_main_queue(), ^{

                    [self snapScreenImage:image controller:self];

            });

        }

    }

    - (void)snapScreenImage:(UIImage*)imagecontroller:(UIViewController*)controller

    {

        CGSizescreenSize = [[UIScreenmainScreen]bounds].size;

        UIGraphicsBeginImageContextWithOptions([[UIScreen mainScreen] bounds].size, NO, 0);

        CGContextRef context = UIGraphicsGetCurrentContext();

        UIGraphicsPushContext(context);

        CGFloatcameraScale =640/480.0;

        CGFloatcameraViewWidth =_camareView.cameraView.bounds.size.width;

        CGFloatcameraViewHeight =_camareView.cameraView.bounds.size.height;

    //    CGFloat preLayerW = cameraViewWidth;

    //    CGFloat preLayerH = preLayerW * cameraScale;

    //    CGFloat preLayerY = -(preLayerH - cameraViewHeight) * 0.5;

        [image drawInRect:CGRectMake(CGRectGetMinX(_camareView.cameraView.frame), CGRectGetMinY(_camareView.cameraView.frame)-20, cameraViewWidth, image.size.height * cameraViewWidth/ image.size.width)];

        UIGraphicsPopContext();

       [self renderView:_camareView inContext:context];

        UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();

        UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil);

        UIGraphicsEndImageContext();

    }

    - (void)renderView:(UIView*)viewinContext:(CGContextRef)context

    {

        // -renderInContext: renders in the coordinate space of the layer,

        // so we must first apply the layer's geometry to the graphics context

        CGContextSaveGState(context);

        // Center the context around the window's anchor point

        CGContextTranslateCTM(context, [view center].x, [view center].y);

        // Apply the window's transform about the anchor point

        CGContextConcatCTM(context, [view transform]);

        // Offset by the portion of the bounds left of and above the anchor point

        CGContextTranslateCTM(context,

                              -[viewbounds].size.width* [[viewlayer]anchorPoint].x,

                              -[viewbounds].size.height* [[viewlayer]anchorPoint].y);

        // Render the layer hierarchy to the current context

        [[viewlayer]renderInContext:context];

    //    [view drawViewHierarchyInRect:view.frame afterScreenUpdates:NO];

        // Restore the context

        CGContextRestoreGState(context);

    }

    ```

    相关文章

      网友评论

          本文标题:AVCaptureVideoPreviewLayer 相机截屏

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