我到處尋找,我無法弄清楚如何以這種方式切換閃光燈,當我拍照時,它會閃爍4次,就像它在iPhone相機上一樣。如何使用閃光燈在自定義相機上拍照?
我使用這個功能可切換閃光燈...
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
}
})
}
}
我認爲這已經在這裏得到解答:http://stackoverflow.com/a/32301753/4503700 –
@CharlesTruluck不,我試圖讓我可以在拍照時加入閃光燈。 –
對不起!我以爲你的意思是沒有工作 –