模拟"拍照"屏幕闪烁

Rob*_*eph 10 animation avfoundation calayer uiview ios

我有一个模拟拍照的AV基金会应用程序(如相机应用程序).通常以下情况适用于我,但不适用于此情况.

此代码在我的视图控制器中的操作中执行.它包含一个全屏UIView(videoPreviewLayer),它附有一个AVCaptureVideoPreviewLayer.动画执行但不显示任何内容.另请注意,我使用的是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];
             }
 ];
Run Code Online (Sandbox Code Playgroud)

以下是我如何附加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]];
Run Code Online (Sandbox Code Playgroud)

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

Rob*_*eph 5

我的问题是从Flash Foundation回调中调用了"flash"代码.回调未在主线程上运行,因此任何UI代码都无法正确执行.那个解决方案是做这样的事情:

dispatch_async(dispatch_get_main_queue(), ^{ /* execute flash code */ });
Run Code Online (Sandbox Code Playgroud)


neo*_*eye 5

您的代码为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()
})
Run Code Online (Sandbox Code Playgroud)