Photo state#

The photo state provides information on current state of photo capture function. It also indicates information such as number of photo taken in session (useful for burst and hyperlapse) and saved media identifiers.

Camera 1#

Sample code to monitor photo capture state:

/// Keep reference on MainCamera peripheral to get notified of changes.
private var cameraRef: Ref<MainCamera>?

/// Monitors and prints photo capture state.
func monitorPhotoCaptureState(drone: Drone) {
    cameraRef = drone.getPeripheral(Peripherals.mainCamera) { camera in
        // called on main thread when the camera peripheral changes
        if let camera = camera {
            let photoState = camera.photoState
            switch photoState.functionState {
            case .unavailable:
                print("Photo capture not available at present")
            case .stopped:
                print("Photo capture stopped")
            case .started:
                let photoCount = photoState.photoCount
                print("Photo capture started, photoCount: \(photoCount)")
            case .stopping:
                print("Photo capture stopping")
            case .errorInternal, .errorInsufficientStorageSpace:
                print("Photo capture error: \(photoState.functionState)")
            }
            if let mediaId = photoState.mediaId {
                print("Identifier of latest saved media: \(mediaId)")
            }
        }
    }
}

Example of output during a photo capture:

Photo capture stopped
Photo capture started, photoCount: 0
Photo capture started, photoCount: 1
Photo capture stopped
Identifier of latest saved media: 10000559

Camera 2#

Photo capture state is monitored with sub-component Camera2Components.photoCapture.

Sample code to monitor photo capture state:

/// Keep reference on Camera2PhotoCapture component to get notified of changes.
private var photoCaptureRef: Ref<Camera2PhotoCapture>?

/// Monitors and prints photo capture state.
func monitorPhotoCaptureState(drone: Drone) {
    if let camera = drone.getPeripheral(Peripherals.mainCamera2) {
        // get photoCapture component reference and register listener
        photoCaptureRef = camera.getComponent(Camera2Components.photoCapture) { [weak self] photoCapture in
            if let photoCapture = photoCapture {
                // print photo capture state
                self?.printPhotoCaptureState(state: photoCapture.state)
            } else {
                // photoCapture component nil, meaning photo capture is unavailable
                print("Photo capture not available at present")
            }
        }
    }
}

/// Prints photo capture state.
func printPhotoCaptureState(state: Camera2PhotoCaptureState) {
    switch state {
    case .stopped(let latestSavedMediaId):
        print("Photo capture stopped")
        if let mediaId = latestSavedMediaId {
            print("Identifier of latest saved media: \(mediaId)")
        }
    case .starting:
        print("Photo capture starting")
    case .started(let startTime, let photoCount, let mediaStorage):
        print("Photo capture started \(photoCount) \(mediaStorage) \(startTime)")
    case .stopping(let reason, let savedMediaId):
        print("Photo capture stopping \(reason)")
        if let mediaId = savedMediaId {
            print("Identifier of saved media: \(mediaId)")
        }
    }
}

Example of output during a photo capture:

Photo capture stopped
Photo capture starting
Photo capture started 0 Optional(internal) 2021-02-05 10:26:41 +0000
Photo capture stopping captureDone
Identifier of saved media: 10000104
Photo capture stopped
Identifier of latest saved media: 10000104