iOS Swift camera mode- how to indicate photo is captured

2.7k views Asked by At

In Camera mode (AVCaptureVideoPreviewLayer) I manage to capture a photo successfully. I would like to indicate this fact to the user- meaning to show him a sudden black flash and a click sound- similar to what he would experience when taking a photo himself.

How do I do that? Is there some built in functionality that does that?

Thanks

2

There are 2 answers

2
Ralfonso On BEST ANSWER

There is no built-in functionality to do this, but it's pretty simple to do on your own by adding a black UIView with alpha set to zero in your camera view hierarchy, then playing system sound and animating the "flash" view's alpha when the photo is captured.

In viewDidLoad, loadView, or wherever you assemble your view hierarchy

// Assuming cameraView contains your previewLayer...
flashView = UIView(frame: <set your frame>)
flashView.alpha = 0
flashView.backgroundColor = UIColor.blackColor()
cameraView.addSubview(flashView)

Then, in your capture completion block

// Animate the "flash"
UIView.animateWithDuration(0.1, delay: 0, options: .Autoreverse, animations: { () -> Void in
    flashView.alpha = 1
}, completion: nil)

// Play the camera shutter system sound
AudioServicesPlayAlertSound(1108)

For more info on the system sounds, see this question: Playing system sound without importing your own.

0
Ahmed Lotfy On

For Swift 4:

@IBOutlet weak var lightView: UIView! //First set lightView hidden in the storyboard

    //MARK: - Take Screenshot Animation
    func flashAnimation(image: UIImage, rect: CGRect) {
        self.view.bringSubview(toFront: lightView)
        lightView.alpha = 0
        lightView.isHidden = false

        UIView.animate(withDuration: 0.1, delay: 0.0, options: [.curveEaseOut], animations: {() -> Void in
            self.lightView.alpha = 1.0
        }, completion: {(finished: Bool) -> Void in
            self.hideFlashView()
        })
    }

    func hideFlashView() {
        UIView.animate(withDuration: 0.1, delay: 0.0, animations: {() -> Void in
            self.lightView.alpha = 0.0
        })
    }