如何使用闪光灯在自定义相机上拍照?
How to use flash to take picture on custom camera?
我到处都看过,但我不知道如何切换闪光灯,这样当我拍照时闪光灯会像在 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 触发它两次。
我到处都看过,但我不知道如何切换闪光灯,这样当我拍照时闪光灯会像在 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 触发它两次。