代码之家  ›  专栏  ›  技术社区  ›  Martin Cowie

AVCaptureVideoPreviewLayer:拍摄快照

  •  13
  • Martin Cowie  · 技术社区  · 15 年前

    我正在尝试模拟在默认相机应用程序中看到的动画,在默认相机应用程序中,相机取景器的快照动画显示在应用程序显示的一角。

    拥有解决此问题的关键的AVCaptureVideoPreviewLayer对象对这些要求不是很开放:尝试在新层中使用..创建它的副本。。

    - (id)initWithLayer:(id)layer
    

    .. 返回一个空层,没有图像快照,所以很明显这里有一些更深的魔力。

    你的线索/嘘声是最受欢迎的。

    M。

    4 回复  |  直到 15 年前
        1
  •  22
  •   Oded Ben Dov    15 年前

    面对同样的不幸,从一个稍微不同的角度。

    以下是可能的解决方案,没有一个是非常好的:

    • AVCaptureSession会话 和一个 AVCaptureVideoData输出 会话预设 AVCaptureSessionPresetHigh高 AVCaptureSessionPresetPhoto照片 你可以拍真实的照片。所以在拍照之前,你可以切换到视频,得到一帧,然后回到相机。主要的警告是,它需要一个“长”的时间(几秒钟)的相机之间切换摄像机和图片相机。

    • 另一种选择是只使用相机输出( AVCaptureStillImageOutput输出 ),并使用 获取手机屏幕截图。然后可以裁剪出控件,只留下图像。如果要在图像上显示UI控件,这会变得很复杂。另外,根据 this 之后,苹果开始拒绝使用这个功能的应用程序(这一直是不确定的)。

    • 除此之外,我还试着玩 . 有 this 上层 ,的 ,的 模型层 ,但无济于事。我猜数据 AVCaptureVideoPreviewLayer 是非常内部的,不是常规层基础设施的一部分。

    希望这有帮助, 奥德。

        2
  •  5
  •   João Colaço    11 年前

    我想你应该加一个 AVCaptureVideoDataOutput

    AVCaptureVideoDataOutput *videoOutput = [[AVCaptureVideoDataOutput alloc] init];
    videoOutput.videoSettings = @{ (NSString *)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_32BGRA) };
    [session addOutput:videoOutput];
    
    dispatch_queue_t queue = dispatch_queue_create("MyQueue", NULL);
    [videoOutput setSampleBufferDelegate:self queue:queue];
    dispatch_release(queue);
    

    - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
    
        UIImage *image = [self imageFromSampleBuffer:sampleBuffer];
        // Add your code here that uses the image.
        dispatch_async(dispatch_get_main_queue(), ^{
            _imageView.image = image;
        });   
    }
    

    AVCaptureVideoData输出 使用:

    videoOutput.minFrameDuration = CMTimeMake(1, 15);
    

    你也可以使用 alwaysDiscardsLateVideoFrames

        3
  •  0
  •   Pierre Houston    15 年前

    有两种方法可以获取预览的帧。。AVCaptureVideoDataOutput&AVCaptureStillImageOutput:)

    您的捕获会话设置为抓取视频帧,然后使用所选帧中的cgimage制作层。如果它是为静像设置的,等到得到你的静态图像,并使你的层从该图像的缩小版本。如果您的会话还没有输出,我认为您必须添加一个输出。

        4
  •  -7
  •   JoJo    10 年前

    从iOS 7开始,您可以使用 UIView::snapshotViewAfterScreenUpdates 为包装AVCaptureVideoPreviewLayer的UIView创建快照。这和 UIGetScreenImage

    UIView *snapshot = [self.containerView snapshotViewAfterScreenUpdates:YES];
    

    回想一下把一个视图变成一个图像的老方法。出于某种原因,除了相机预览,它对所有东西都有效:

    UIGraphicsBeginImageContextWithOptions(self.containerView.bounds.size, NO, [UIScreen mainScreen].scale);
    [self.containerView drawViewHierarchyInRect:self.containerView.bounds afterScreenUpdates:YES];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();