Search code examples
swiftcameraresolutionavcapturesessionpreset

How to check if AVCaptureSession preset resolution is supported in Swift


For instance, I may want to set it to hd1280x720, but only if it's supported, and use a lower res that is supported otherwise.

session = AVCaptureSession()
if(**my device supports hd1280x720**){
  session!.sessionPreset = AVCaptureSession.Preset.hd1280x720
}else if(**my device supports 640x480**){
  session!.sessionPreset = AVCaptureSession.Preset.vga640x480
}

Solution

  • Use AVCaptureSession.canSetSessionPreset(_:):

    Returns a Boolean value that indicates whether the receiver can use the given preset.