Class

AVCaptureDevice

A device that provides input (such as audio or video) for capture sessions and offers controls for hardware-specific capture features.

Declaration

class AVCaptureDevice : NSObject

Overview

An AVCaptureDevice object represents a physical capture device and the properties associated with that device. You use a capture device to configure the properties of the underlying hardware. A capture device also provides input data (such as audio or video) to an AVCaptureSession object.

You use the methods of the AVCaptureDevice class to enumerate the available devices, query their capabilities, and be informed about when devices come and go. Before you attempt to set properties of a capture device (its focus mode, exposure mode, and so on), you must first acquire a lock on the device using the lockForConfiguration() method. You should also query the device’s capabilities to ensure that the new modes you intend to set are valid for that device. You can then set the properties and release the lock using the unlockForConfiguration() method. You may hold the lock if you want all settable device properties to remain unchanged. However, holding the device lock unnecessarily may degrade capture quality in other applications sharing the device and is not recommended.

Most common configurations of capture settings are available through the AVCaptureSession object and its available presets. However, on iOS devices, some specialized options (such as high frame rate) require directly setting a capture format on an AVCaptureDevice instance. The following code example illustrates how to select an iOS device’s highest possible frame rate:

func configureCameraForHighestFrameRate(device: AVCaptureDevice) {
    
    var bestFormat: AVCaptureDevice.Format?
    var bestFrameRateRange: AVFrameRateRange?

    for format in device.formats {
        for range in format.videoSupportedFrameRateRanges {
            if range.maxFrameRate > bestFrameRateRange?.maxFrameRate ?? 0 {
                bestFormat = format
                bestFrameRateRange = range
            }
        }
    }
    
    if let bestFormat = bestFormat, 
       let bestFrameRateRange = bestFrameRateRange {
        do {
            try device.lockForConfiguration()
            
            // Set the device's active format.
            device.activeFormat = bestFormat
            
            // Set the device's min/max frame duration.
            let duration = bestFrameRateRange.minFrameDuration
            device.activeVideoMinFrameDuration = duration
            device.activeVideoMaxFrameDuration = duration
            
            device.unlockForConfiguration()
        } catch {
            // Handle error.
        }
    }
}

Topics

Discovering Devices

init?(uniqueID: String)

Returns the device with a given ID.

class func `default`(for: AVMediaType) -> AVCaptureDevice?

Returns the default device used to capture data of a given media type.

class func `default`(AVCaptureDevice.DeviceType, for: AVMediaType?, position: AVCaptureDevice.Position) -> AVCaptureDevice?

Returns the default device for the specified device type, media type, and position.

enum AVCaptureDevice.Position

Constants to specify the position of a capture device.

struct AVCaptureDevice.DeviceType

Values identifying the capture device’s type.

class AVCaptureDevice.DiscoverySession

A query for finding and monitoring available capture devices.

static let AVCaptureDeviceWasConnected: NSNotification.Name

Posted when a new device becomes available.

static let AVCaptureDeviceWasDisconnected: NSNotification.Name

Posted when an existing device becomes unavailable.

class func devices() -> [AVCaptureDevice]

Returns an array of the available capture devices on the system.

Deprecated
class func devices(for: AVMediaType) -> [AVCaptureDevice]

Returns an array of the devices able to capture data of a given media type.

Deprecated

Verifying Authorization

class func requestAccess(for: AVMediaType, completionHandler: (Bool) -> Void)

Requests the user’s permission, if needed, for recording a specified media type.

class func authorizationStatus(for: AVMediaType) -> AVAuthorizationStatus

Returns a constant indicating whether the app has permission for recording a specified media type.

enum AVAuthorizationStatus

Constants that provide information regarding permission to use media capture devices.

Configuring the Device

func lockForConfiguration()

Requests exclusive access to the device’s hardware properties.

func unlockForConfiguration()

Relinquishes exclusive control over the device’s configuration.

var isInUseByAnotherApplication: Bool

Indicates whether the device is being used exclusively by another application.

var isSuspended: Bool

Indicates whether the device is suspended.

var linkedDevices: [AVCaptureDevice]

An array of AVCaptureDevice objects representing the devices physically linked to the receiver.

var transportType: Int32

The transport type of the receiver.

class AVCaptureDevice.InputSource

A distinct input source on a capture device.

var inputSources: [AVCaptureDevice.InputSource]

An array of AVCaptureDevice.InputSource objects representing the input sources supported by the device.

var activeInputSource: AVCaptureDevice.InputSource?

The currently active input source.

Inspecting Device Characteristics

var uniqueID: String

An ID unique to the model of device corresponding to the receiver.

var modelID: String

The model ID of the device.

var manufacturer: String

A human-readable string describing the manufacturer of the device.

var localizedName: String

A localized, human-readable name for the receiver.

var isConnected: Bool

Indicates whether the device is currently connected.

var isVirtualDevice: Bool

A Boolean value that indicates whether the device is a virtual device consisting of two or more physical devices.

Beta
var deviceType: AVCaptureDevice.DeviceType

The general type of the device, such as a built-in microphone or wide-angle camera.

var constituentDevices: [AVCaptureDevice]

An array of physical devices comprising a virtual device.

Beta
var lensAperture: Float

The size of the lens diaphragm.

var position: AVCaptureDevice.Position

Indicates the physical position of the device hardware on the system.

func hasMediaType(AVMediaType) -> Bool

Returns a Boolean value that indicates whether the device provides media with a given type.

func supportsSessionPreset(AVCaptureSession.Preset) -> Bool

Returns a Boolean value that indicates whether the receiver can be used in a capture session configured with the given preset.

Managing Formats

var formats: [AVCaptureDevice.Format]

The capture formats supported by the device.

var activeFormat: AVCaptureDevice.Format

The currently active media data format of the capture device.

var activeDepthDataFormat: AVCaptureDevice.Format?

The currently active depth data format of the capture device.

class AVCaptureDevice.Format

A set of media format and capture settings (such as video resolution and frame rate) that you use to configure a capture device.

Managing Image Exposure

var exposureDuration: CMTime

The length of time over which exposure takes place.

var exposureTargetOffset: Float

The metered exposure level's offset from the target exposure value, in EV units.

var exposureTargetBias: Float

Bias applied to the target exposure value, in EV units.

var minExposureTargetBias: Float

The minimum supported exposure bias, in EV units.

var maxExposureTargetBias: Float

The maximum supported exposure bias, in EV units.

var activeMaxExposureDuration: CMTime

The maximum exposure duration, in seconds, defined in the auto-exposure algorithm.

func setExposureTargetBias(Float, completionHandler: ((CMTime) -> Void)?)

Sets the bias to be applied to the target exposure value.

class let currentExposureTargetBias: Float

A special constant representing the current exposure bias value.

class let currentExposureDuration: CMTime

A special constant representing the current exposure duration setting.

Managing Exposure Settings

var isAdjustingExposure: Bool

Indicates whether the device is currently adjusting its exposure setting.

var exposureMode: AVCaptureDevice.ExposureMode

The exposure mode for the device.

func isExposureModeSupported(AVCaptureDevice.ExposureMode) -> Bool

Returns a Boolean value that indicates whether the given exposure mode is supported.

var exposurePointOfInterest: CGPoint

The point of interest for exposure.

var isExposurePointOfInterestSupported: Bool

Indicates whether the device supports a point of interest for exposure.

enum AVCaptureDevice.ExposureMode

Constants to specify the exposure mode of a capture device.

Managing Zoom Settings

var videoZoomFactor: CGFloat

A value that controls the cropping and enlargement of images captured by the device.

var minAvailableVideoZoomFactor: CGFloat

The minimum zoom factor allowed in the current capture configuration.

var maxAvailableVideoZoomFactor: CGFloat

The maximum zoom factor allowed in the current capture configuration.

func ramp(toVideoZoomFactor: CGFloat, withRate: Float)

Begins a smooth transition from the current zoom factor to another.

var isRampingVideoZoom: Bool

A Boolean value that indicates whether a zoom transition is in progress.

func cancelVideoZoomRamp()

Smoothly ends a zoom transition in progress.

var virtualDeviceSwitchOverVideoZoomFactors: [NSNumber]

An array of video zoom factors at or above which a virtual device, such as the dual camera, may switch to its next constituent device.

Beta
var dualCameraSwitchOverVideoZoomFactor: CGFloat

The video zoom factor at which a dual camera device can automatically switch between cameras.

Deprecated

Managing Focus Settings

var focusMode: AVCaptureDevice.FocusMode

The capture device’s focus mode.

func isFocusModeSupported(AVCaptureDevice.FocusMode) -> Bool

Returns a Boolean value that indicates whether the device supports the given focus mode.

var focusPointOfInterest: CGPoint

The point of interest for focusing.

var isFocusPointOfInterestSupported: Bool

Indicates whether the device supports a point of interest for focus.

var isAdjustingFocus: Bool

Indicates whether the device is currently adjusting its focus setting.

var isSmoothAutoFocusEnabled: Bool

A Boolean value that determines whether the device has smooth autofocus enabled.

var isSmoothAutoFocusSupported: Bool

A Boolean value that indicates whether the device supports smooth autofocus.

var autoFocusRangeRestriction: AVCaptureDevice.AutoFocusRangeRestriction

A value controlling the allowable range for automatic focusing.

var isAutoFocusRangeRestrictionSupported: Bool

A Boolean value that indicates whether the device supports focus range restrictions.

enum AVCaptureDevice.FocusMode

Constants to specify the focus mode of a capture device.

enum AVCaptureDevice.AutoFocusRangeRestriction

Constants to specify the autofocus range of a capture device.

Managing Flash Settings

var hasFlash: Bool

Indicates whether the capture device has a flash.

var isFlashAvailable: Bool

Indicates whether the flash is currently available for use.

enum AVCaptureDevice.FlashMode

Constants to specify the flash mode of a capture device.

var flashMode: AVCaptureDevice.FlashMode

The device’s current flash mode.

func isFlashModeSupported(AVCaptureDevice.FlashMode) -> Bool

Returns a Boolean value that indicates whether the device supports the given flash mode.

var isFlashActive: Bool

Indicates whether the flash is currently active.

Deprecated

Managing Torch Settings

var hasTorch: Bool

A Boolean value that specifies whether the capture device has a torch.

var isTorchAvailable: Bool

Indicates whether the torch is currently available for use.

var isTorchActive: Bool

A Boolean value indicating whether the device’s torch is currently active.

var torchLevel: Float

The current torch brightness level.

enum AVCaptureDevice.TorchMode

Constants to specify the capture device’s torch mode.

func isTorchModeSupported(AVCaptureDevice.TorchMode) -> Bool

Returns a Boolean value that indicates whether the device supports the specified torch mode.

func setTorchModeOn(level: Float)

Sets the illumination level when in torch mode.

Managing Low Light Settings

var isLowLightBoostSupported: Bool

A Boolean value that indicates whether the capture device supports boosting images in low light conditions.

var isLowLightBoostEnabled: Bool

A Boolean value that indicates whether the capture device’s low light boost feature is enabled.

var automaticallyEnablesLowLightBoostWhenAvailable: Bool

A Boolean value that indicates whether the capture device should automatically switch to low light boost mode when necessary.

Managing Frame Rate Settings

var activeVideoMinFrameDuration: CMTime

The currently active minimum frame duration.

var activeVideoMaxFrameDuration: CMTime

The currently active maximum frame duration

Managing Transport Controls

var transportControlsSupported: Bool

Indicates whether the device supports transport control commands.

enum AVCaptureDevice.TransportControlsPlaybackMode

Constants that indicate the transport control's current mode of playback, if it has one.

Monitoring Subject Area Change

var isSubjectAreaChangeMonitoringEnabled: Bool

Indicates whether the device should monitor the subject area for changes.

static let AVCaptureDeviceSubjectAreaDidChange: NSNotification.Name

Posted when the instance of AVCaptureDevice has detected a substantial change to the video subject area.

Managing the Lens Position

var lensPosition: Float

Indicates the focus position of the lens.

var isLockingFocusWithCustomLensPositionSupported: Bool

A Boolean value indicating whether the device supports locking focus to a specific lens position.

class let currentLensPosition: Float

A special constant representing the current lens position.

Managing the White Balance

func isWhiteBalanceModeSupported(AVCaptureDevice.WhiteBalanceMode) -> Bool

Returns a Boolean value that indicates whether the given white balance mode is supported.

var isAdjustingWhiteBalance: Bool

Indicates whether the devise is currently adjusting the white balance.

func chromaticityValues(for: AVCaptureDevice.WhiteBalanceGains) -> AVCaptureDevice.WhiteBalanceChromaticityValues

Converts device-specific white balance RGB gain values to device-independent chromaticity values.

var deviceWhiteBalanceGains: AVCaptureDevice.WhiteBalanceGains

The current device-specific RGB white balance gain values.

var maxWhiteBalanceGain: Float

The maximum supported value to which a channel in the AVCaptureWhiteBalanceGains may be set.

func deviceWhiteBalanceGains(for: AVCaptureDevice.WhiteBalanceChromaticityValues) -> AVCaptureDevice.WhiteBalanceGains

Converts device-independent chromaticity values to device-specific white balance RGB gain values.

func deviceWhiteBalanceGains(for: AVCaptureDevice.WhiteBalanceTemperatureAndTintValues) -> AVCaptureDevice.WhiteBalanceGains

Converts device-independent temperature and tint values to device-specific white balance RGB gain values.

var grayWorldDeviceWhiteBalanceGains: AVCaptureDevice.WhiteBalanceGains

The current device-specific white balance values required for a neutral gray white point.

func setWhiteBalanceModeLocked(with: AVCaptureDevice.WhiteBalanceGains, completionHandler: ((CMTime) -> Void)?)

Sets white balance to locked mode with the specified deviceWhiteBalanceGains values.

func temperatureAndTintValues(for: AVCaptureDevice.WhiteBalanceGains) -> AVCaptureDevice.WhiteBalanceTemperatureAndTintValues

Converts device-specific white balance RGB gain values to device-independent temperature and tint values.

var isLockingWhiteBalanceWithCustomDeviceGainsSupported: Bool

A Boolean value indicating whether the device supports locking white balance to specific gain values.

class let currentWhiteBalanceGains: AVCaptureDevice.WhiteBalanceGains

A special constant representing the current white balance setting.

struct AVCaptureDevice.WhiteBalanceGains

Structure containing RGB white balance gain values.

struct AVCaptureDevice.WhiteBalanceChromaticityValues

Structure containing CIE 1931 xy chromaticity values.

struct AVCaptureDevice.WhiteBalanceTemperatureAndTintValues

Structure containing temperature and tint values correlated to a white-balance color.

enum AVCaptureDevice.WhiteBalanceMode

Constants to specify the white balance mode of a capture device.

Managing the ISO

var iso: Float

The current exposure ISO value.

class let currentISO: Float

This value indicates that the caller does not wish to specify a value for the iso property, and that it should instead be set to its current value.

Managing High Dynamic Range Video

var automaticallyAdjustsVideoHDREnabled: Bool

A Boolean value that indicates whether the capture device is allowed to turn high dynamic range streaming on or off.

var isVideoHDREnabled: Bool

A Boolean value that indicates whether the receiver is allowed to turn high dynamic range streaming on or off.

Managing Color Spaces

var activeColorSpace: AVCaptureColorSpace

The currently active color space for capture.

enum AVCaptureColorSpace

Constants to specify the color gamut and format for image and video capture, used with the activeColorSpace property.

Managing Geometric Distortion

var isGeometricDistortionCorrectionSupported: Bool

A Boolean value that indicates whether this device supports geometric distortion correction.

Beta
var isGeometricDistortionCorrectionEnabled: Bool

A Boolean value that indicates whether geometric distortion correction is enabled for this device.

Beta

Managing Device Calibration

class func extrinsicMatrix(from: AVCaptureDevice, to: AVCaptureDevice) -> Data?

The relative extrinsic matrix from one capture device to another.

Beta

Managing System Pressure Changes

var systemPressureState: AVCaptureDevice.SystemPressureState

The current of OS and hardware status affecting capture system performance and availability.

class AVCaptureDevice.SystemPressureState

Information about OS and hardware status affecting capture system performance and availability.

let AVCaptureSessionInterruptionSystemPressureStateKey: String

The key for retrieving information about system pressure factors that caused a capture session interruption.

Managing Tone Mapping

var isGlobalToneMappingEnabled: Bool

A Boolean value that indicates whether the device should use global tone mapping.

Beta

Relationships

Inherits From

Conforms To

See Also

Capture Devices

Choosing a Capture Device

Select the front or back camera, or use advanced features like the TrueDepth camera or dual camera.

class AVCaptureDeviceInput

A capture input that provides media from a capture device to a capture session.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software