2012-10-16 34 views
9

Tôi có ứng dụng AV Foundation mô phỏng chụp ảnh (như ứng dụng máy ảnh). Thông thường các công trình sau đây cho tôi, nhưng không phải trong trường hợp này.Mô phỏng đèn flash "chụp ảnh"

Mã này thực thi trong một hành động trong trình điều khiển chế độ xem của tôi. Nó chứa một UIView toàn màn hình duy nhất (videoPreviewLayer) có kèm theo AVCaptureVideoPreviewLayer. Hoạt ảnh thực hiện nhưng không có gì được hiển thị. Cũng lưu ý rằng tôi đang sử dụng 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]; 
      } 
]; 

Sau đây là cách tôi đính kèm các 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]]; 

LƯU Ý Sau khi điều tra thêm, đèn flash đang xảy ra mặc dù không liên tục. Nói chung nó dường như trở nên có thể nhìn thấy khoảng 5-10 giây sau khi nó bắt đầu. Tôi cũng thấy nó xảy ra chạy hai lần liên tiếp ngay cả khi tôi gọi mã một lần.

Trả lời

4

Vấn đề của tôi là mã "flash" đã được gọi từ cuộc gọi lại của Quỹ AV. Cuộc gọi lại không chạy trên luồng chính, do đó bất kỳ mã giao diện người dùng nào sẽ không thực thi chính xác. Giải pháp đó là phải làm một cái gì đó như thế này:

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

mã của bạn như 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() 
}) 
Các vấn đề liên quan