ios - 如何使用闪光灯在自定义相机上拍照?

标签 ios iphone swift camera avcapturesession

我到处都看过,但我不知道如何以这种方式切换闪光灯,当我拍照时它会像在 iPhone 相机上那样闪烁 4 次。

我正在使用这个功能来切换闪光灯...

func toggleFlash() {
    let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
    if (device.hasTorch) {
        device.lockForConfiguration(nil)
        if (device.torchMode == AVCaptureTorchMode.On) {
            device.torchMode = AVCaptureTorchMode.Off
        } else {
            device.setTorchModeOnWithLevel(1.0, error: nil)
        }
        device.unlockForConfiguration()
    }
}

然后我用这个功能拍照...

func didPressTakePhoto(){
    if let videoConnection = stillImageOutput?.connectionWithMediaType(AVMediaTypeVideo){
        videoConnection.videoOrientation = AVCaptureVideoOrientation.Portrait
        stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: {
            (sampleBuffer, error) in

            if sampleBuffer != nil {

                var imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
                var dataProvider  = CGDataProviderCreateWithCFData(imageData)
                var cgImageRef = CGImageCreateWithJPEGDataProvider(dataProvider, nil, true, kCGRenderingIntentDefault)

                var image:UIImage!

                if self.camera == true {
                    image = UIImage(CGImage: cgImageRef, scale: 1.0, orientation: UIImageOrientation.Right)

                } else {
                    image = UIImage(CGImage: cgImageRef, scale: 1.0, orientation: UIImageOrientation.LeftMirrored)

                }
                self.tempImageView.image = image
                self.tempImageView.hidden = false
            }


        })
    }


}

最佳答案

这是一个非常困惑的方式。不干净。

func didPressTakePhoto(){

toggleFlash()
sleep(1)
toggleFlash()

if let videoConnection = stillImageOutput?.connectionWithMediaType(AVMediaTypeVideo){
    videoConnection.videoOrientation = AVCaptureVideoOrientation.Portrait
    stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: {
        (sampleBuffer, error) in

        if sampleBuffer != nil {

            toggleFlash()

            var imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
            var dataProvider  = CGDataProviderCreateWithCFData(imageData)
            var cgImageRef = CGImageCreateWithJPEGDataProvider(dataProvider, nil, true, kCGRenderingIntentDefault)

            var image:UIImage!
            if self.camera == true {
                image = UIImage(CGImage: cgImageRef, scale: 1.0, orientation: UIImageOrientation.Right)

            } else {
                image = UIImage(CGImage: cgImageRef, scale: 1.0, orientation: UIImageOrientation.LeftMirrored)

            }
            self.tempImageView.image = image
            self.tempImageView.hidden = false
        }


    })
}


}

很乱,只是一个idea。 您也可以使用 NSTimer 触发它两次。

关于ios - 如何使用闪光灯在自定义相机上拍照?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33083006/

相关文章:

ios - 如何根据内容创建具有动态单元格高度的 UITableViewCell

iphone - 手势识别器,奇怪的行为(乘数效应)iOS

iphone - 薄弱环节框架

iphone - 在使用 ASIHTTPRequest 请求图像期间何时收到响应代码?

ios - 将 TLS V1.1/V 1.2 与 NSURLConnection 结合使用

ios - UITableView 在创建单元格时抛出 NSUnkownKeyException

iphone - 释放内存分配的正确方法?

swift - 如何捕获 Mac Catalyst 应用程序上的右键单击?

xcode - 从另一个 View Controller 访问数组

swift - 为什么 DBL_MIN 不是负数(在 Swift 中)?