【问题标题】:Simulate a "picture taken" screen flash模拟“拍照”屏幕闪烁
【发布时间】:2012-10-07 02:34:47
【问题描述】:

我有一个模拟拍照的 AV Foundation 应用程序(如相机应用程序)。通常以下内容对我有用,但在这种情况下不是。

这段代码在我的视图控制器的一个动作中执行。它包含一个附加了 AVCaptureVideoPreviewLayer 的全屏 UIView (videoPreviewLayer)。动画执行,但没有显示任何内容。另请注意,我使用的是 ARC、iOS 6、iPhone 4S、iPad3。

// Flash the screen white and fade it out
UIView *flashView = [[UIView alloc] initWithFrame:[[self videoPreviewView] frame]]; 
[flashView setBackgroundColor:[UIColor whiteColor]];
[[[self view] window] addSubview:flashView];

[UIView animateWithDuration:1.f
             animations:^{
                 [flashView setAlpha:0.f];
             }
             completion:^(BOOL finished){
                 [flashView removeFromSuperview];
             }
 ];

这是我附加 AVCaptureVideoPreviewLayer 的方法:

 // Setup our preview layer so that we can display video from the camera
captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];

CALayer *viewLayer = videoPreviewView.layer;
viewLayer.masksToBounds = YES;

captureVideoPreviewLayer.frame = videoPreviewView.bounds;

[viewLayer insertSublayer:captureVideoPreviewLayer below:[[viewLayer sublayers] objectAtIndex:0]];

注意 经过进一步调查,闪光虽然间歇性地发生。通常它似乎在它应该开始后大约 5-10 秒变得可见。即使我调用了一次代码,我也看到它快速连续运行两次。

【问题讨论】:

    标签: ios animation uiview avfoundation calayer


    【解决方案1】:

    我的问题是“flash”代码是从 AV Foundation 回调中调用的。回调未在主线程上运行,因此任何 UI 代码都无法正确执行。该解决方案是执行以下操作:

    dispatch_async(dispatch_get_main_queue(), ^{ /* execute flash code */ });
    

    【讨论】:

      【解决方案2】:

      你的代码是 swift3

      let shutterView = UIView(frame: cameraView.frame)
      shutterView.backgroundColor = UIColor.black
      view.addSubview(shutterView)
      UIView.animate(withDuration: 0.3, animations: {
          shutterView.alpha = 0
      }, completion: { (_) in
          shutterView.removeFromSuperview()
      })
      

      【讨论】:

        猜你喜欢
        • 2022-10-20
        • 2022-10-20
        • 2021-10-04
        • 2011-09-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多