2012-10-16 7 views
9

저는 (카메라 앱과 같은) 사진 촬영을 시뮬레이트하는 AV Foundation 앱이 있습니다. 일반적으로 다음은 나를 위해 작동하지만이 경우에는 아닙니다."사진 찍기"화면을 시뮬레이션하십시오.

이 코드는 내 View Controller의 동작 내에서 실행됩니다. 그것은 AVCaptureVideoPreviewLayer가 첨부 된 하나의 전체 화면 UIView (videoPreviewLayer)를 포함합니다. 애니메이션은 실행되지만 아무 것도 표시되지 않습니다. ARC, iOS 6, iPhone 4S, iPad3을 사용 중입니다. 추가 조사 후

// 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]]; 

참고은 플래시가 간헐적으로하지만 발생 : 여기

// 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를 부착하는 방법입니다. 일반적으로 시작해야 약 5-10 초 후에 표시되는 것으로 보입니다. 한 번 코드를 호출하더라도 빠르게 연속해서 두 번 실행되는 것을 보았습니다.

답변

4

내 문제는 "플래시"코드가 AV Foundation 콜백에서 호출되고 있다는 것이 었습니다. 콜백이 주 스레드에서 실행되지 않아 UI 코드가 제대로 실행되지 않습니다.

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

코드

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() 
}) 
swift3 등 : 그 솔루션은 이런 일을 할 수 있었다
관련 문제