我正在做一个类似Instagram的应用程序,我在ATM上有一个工作代码,可以捕获照片并将其保存在设备的照片库中。摄像机预览用作后/前摄像机开关。我现在遇到的问题是,如果我按下按钮,则在捕获功能中提供一段代码即可将闪光灯设置为.auto或.off,因此当我拍摄照片时,闪光灯将可以工作。这是我的捕获代码:
func capture(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhotoSampleBuffer photoSampleBuffer: CMSampleBuffer?, previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) {
if let error = error {
print("error occure : \(error.localizedDescription)")
}
if let sampleBuffer = photoSampleBuffer,
let previewBuffer = previewPhotoSampleBuffer,
let dataImage = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: sampleBuffer, previewPhotoSampleBuffer: previewBuffer) {
print(UIImage(data: dataImage)?.size as Any)
let dataProvider = CGDataProvider(data: dataImage as CFData)
let cgImageRef: CGImage! = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: .defaultIntent)
let image = UIImage(cgImage: cgImageRef, scale: 1.0, orientation: UIImageOrientation.right)
UIImageWriteToSavedPhotosAlbum(image, self, nil, nil)
self.cameracapture.image = image
} else {
print("some error here")
}
}
最佳答案
这是我使用的代码:
let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
if device.hasTorch {
do {
try device.lockForConfiguration()
device.torchMode = AVCaptureTorchMode.On
device.torchMode = AVCaptureTorchMode.Off
device.torchMode = AVCaptureTorchMode.Auto
device.unlockForConfiguration()
} catch {
print(error)
}
}
希望对您有所帮助。 :)
关于ios - AVFoundation-如何在拍摄图像之前将闪光灯设置为自动/关闭,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42559271/