Skip to content

Instantly share code, notes, and snippets.

@simonkim
Created June 8, 2018 05:46
Show Gist options
  • Save simonkim/4c7a20fb978c9dfc350b6bb4c1512332 to your computer and use it in GitHub Desktop.
Save simonkim/4c7a20fb978c9dfc350b6bb4c1512332 to your computer and use it in GitHub Desktop.
How to check if an iOS camera support 4K capture
/*
* Does not require camera access permission
* Usage:
let tool = CaptureDeviceDiscovery()
let front = tool.videoCaptureDevice(at: .front)?.supportsSessionPreset(AVCaptureSession.Preset.hd4K3840x2160) ?? false
let back = tool.videoCaptureDevice(at: .back)?.supportsSessionPreset(AVCaptureSession.Preset.hd4K3840x2160) ?? false
self.labelStatusFront.text = front ? "Supported" : "Not supported"
self.labelStatusBack.text = back ? "Supported" : "Not supported"
*/
class CaptureDeviceDiscovery {
@available(iOS 10.0, *)
func availableDeviceTypes() -> [AVCaptureDevice.DeviceType] {
var deviceTypes: [AVCaptureDevice.DeviceType] = [.builtInTelephotoCamera, .builtInWideAngleCamera]
// 10.2
if #available(iOS 10.2, *) {
deviceTypes.append(.builtInDualCamera)
if #available(iOS 11.1, *) {
deviceTypes.append(.builtInTrueDepthCamera)
}
}
return deviceTypes
}
func videoCaptureDevice(at position: AVCaptureDevice.Position) -> AVCaptureDevice? {
var device: AVCaptureDevice? = nil
if #available(iOS 10, *) {
let deviceTypes = availableDeviceTypes()
let session = AVCaptureDevice.DiscoverySession(deviceTypes: deviceTypes, mediaType: .video, position: position)
device = session.devices.first
} else {
device = AVCaptureDevice.devices(for: AVMediaType.video).filter( { $0.position == position }).first
}
return device
}
}
@unobatbayar
Copy link

Thank you! Is it possible to use this code? I'll reference you.

@simonkim
Copy link
Author

simonkim commented Feb 1, 2023

Thank you! Is it possible to use this code? I'll reference you.

Sure. As much as you want

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment