iOS Swift相机模式 - 如何捕捉照片

时间:2015-06-12 21:26:43

标签: ios swift camera photo capture

在相机模式(AVCaptureVideoPreviewLayer)中,我设法成功拍摄照片。我想向用户表明这一事实 - 意思是向他展示突然的黑色闪光和咔哒声 - 类似于他自己拍照时的体验。

我该怎么做?是否有一些内置功能可以做到这一点?

由于

2 个答案:

答案 0 :(得分:3)

没有内置功能可以做到这一点,但通过在摄像机视图层次结构中添加alpha设置为零的黑色UIView,然后播放系统声音并设置动画,您可以自行完成这项功能非常简单"闪速"拍摄照片时查看阿尔法。

viewDidLoadloadView或您组合视图层次结构的任何地方

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

然后,在捕获完成块中

// 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)

有关系统声音的详细信息,请参阅此问题:Playing system sound without importing your own

答案 1 :(得分:1)

对于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
        })
    }