Manually set exposure for iOS camera in Swift

Viewed 7053

I understand that the camera in iOS automatically adjusts exposure continuously when capturing video and photos.

Questions:

How can I turn off the camera's automatic exposure?

In Swift code, how can I set the exposure for the camera to "zero" so that exposure is completely neutral to the surroundings and not compensating for light?

2 Answers

If you need only min, current and max exposure values, then you can use the following:

Swift 5

    import AVFoundation

    enum Esposure {
        case min, normal, max
        
        func value(device: AVCaptureDevice) -> Float {
            switch self {
            case .min:
                return device.activeFormat.minISO
            case .normal:
                return AVCaptureDevice.currentISO
            case .max:
                return device.activeFormat.maxISO
            }
        }
    }

    func set(exposure: Esposure) {
        guard let device = AVCaptureDevice.default(for: AVMediaType.video) else { return }
        if device.isExposureModeSupported(.custom) {
            do{
                try device.lockForConfiguration()
                device.setExposureModeCustom(duration: AVCaptureDevice.currentExposureDuration, iso: exposure.value(device: device)) { (_) in
                    print("Done Esposure")
                }
                device.unlockForConfiguration()
            }
            catch{
                print("ERROR: \(String(describing: error.localizedDescription))")
            }
        }
    }
Related