简体   繁体   English

DJI SDK获取最后一张图片

[英]DJI SDK Get last image

I am currently trying to access the last image taken by a DJI Phantom 4 using the mobile SDK. 我目前正在尝试使用移动SDK访问DJI Phantom 4拍摄的最后一张图像。 I have looked at: How to programmatically download Images from drone using the IOS DJI-SDK and it has helped, however, while in the .refreshFileList() call, an error is thrown that says 'Execution of this process has timed out(code -1003)'. 我看过: 如何使用IOS DJI-SDK以编程方式从无人机下载图像 ,它有所帮助,但是,在.refreshFileList()调用中,引发了一个错误,指出``此过程的执行已超时(代码-1003)”。

Any help would be greatly appreciated! 任何帮助将不胜感激! Here is my code: 这是我的代码:

/***** Setup Camera *****/

// get current product
guard let drone = DJISDKManager.product() else {
    print("Product is connected but DJISDKManager.product is nil when attempting to download media")
    return
}

// Get camera on drone
guard  let camera: DJICamera = drone.camera else {
    print("Unable to detect Camera in initDownload()")
    return
}

print("Successfully detected the camera")


// take picture when project starts
camera.startShootPhoto(completion: { (error) in

    if (error != nil) {
        print("Shoot photo error: \(error.debugDescription)")
    }

})


/***** Get Last Picture *****/

// check if we can download images with the product
if !camera.isMediaDownloadModeSupported() {
    print("Product does not support media download mode")
    return
}

print("before set mode...")

// switch camera mode to allow for media downloads
camera.setMode( .mediaDownload, withCompletion: {(error) in

    print("in set mode...")

    if error != nil {

        print(("\(error!.localizedDescription)"))

    } else {

        // get the media manager from the drone to gain access to the files
        let manager = camera.mediaManager!

        manager.refreshFileList(of: DJICameraStorageLocation.sdCard, withCompletion:  { (error) in

            print("in refresh file list...")

            if error != nil {

                ///////TIMES OUT HERE/////////

                print("Refresh error State: \(manager.sdCardFileListState.rawValue)")
                print("Error refreshing list: \(error!.localizedDescription)")

            }else {

                print("Refreshed file list")
                print("No error State: \(manager.sdCardFileListState.rawValue)")

                // get list of files
                guard let files = manager.sdCardFileListSnapshot() else {
                    print("No files to download")
                    return
                }

                print("There are files to download.. Beginning Download")
                print(("files \(files.count)"))
            }
        }) // end of file-refresh block
    } // end of if else
})// end of camera setMode block

The issue with your code is that you are not waiting for the completion of the startShootPhoto action to finish before changing modes. 代码的问题在于,您在更改模式之前没有等待startShootPhoto操作完成。 This might be what is causing your error. 这可能是导致您出错的原因。

Here is working version of your function 这是您功能的工作版本

 func shootPhoto() {
    guard let drone = (DJISDKManager.product() as? DJIAircraft) else {
        print("Product is connected but DJISDKManager.product is nil when attempting to download media")
        return
    }

    // Get camera on drone
    guard  let camera: DJICamera = drone.camera else {
        print("Unable to detect Camera in initDownload()")
        return
    }

    print("Successfully detected the camera")


    // take picture when project starts
    camera.startShootPhoto(completion: { (error) in

        if (error != nil) {
            print("Shoot photo error: \(error.debugDescription)")
        }
        else {
            self.getLastImage(camera: camera)
        }

    })

}

func getLastImage(camera: DJICamera) {
    /***** Get Last Picture *****/

    // check if we can download images with the product
    if !camera.isMediaDownloadModeSupported() {
        print("Product does not support media download mode")
        return
    }

    print("before set mode...")
    let retry = RetryManager() // Run the same block multiple times if the command has an error
    // switch camera mode to allow for media downloads
    retry.runBlock(withRetries: 5) {
        camera.setMode( .mediaDownload, withCompletion: {(error) in

            print("in set mode...")

            if error != nil {

                print(("\(error!.localizedDescription)"))

            } else {
                retry.stop()
                // get the media manager from the drone to gain access to the files
                let manager = camera.mediaManager!

                manager.refreshFileList(of: DJICameraStorageLocation.sdCard, withCompletion:  { (error) in

                    print("in refresh file list...")

                    if error != nil {

                        ///////TIMES OUT HERE/////////

                        print("Refresh error State: \(manager.sdCardFileListState.rawValue)")
                        print("Error refreshing list: \(error!.localizedDescription)")

                    }else {

                        print("Refreshed file list")
                        print("No error State: \(manager.sdCardFileListState.rawValue)")

                        // get list of files
                        guard let files = manager.sdCardFileListSnapshot() else {
                            print("No files to download")
                            return
                        }

                        print("There are files to download.. Beginning Download")
                        print(("files \(files.count)"))
                    }
                }) // end of file-refresh block
            } // end of if else
            retry.proceed()
        })// end of camera setMode block
    }

}

This code works for me, here is the gist for the retry manager, I had to use it since the first couple attempts failed. 该代码对我有用,这是重试管理器的要点,因为前几次尝试均失败,所以我不得不使用它。 https://gist.github.com/justinmiller62/4c76d9704524fd8aaa60e166a5cccea8 https://gist.github.com/justinmiller62/4c76d9704524fd8aaa60e166a5cccea8

Additionally if you want your code inline instead of nested like this, you can use semaphores to wait until the previous function is complete. 另外,如果您希望代码内联而不是像这样嵌套,则可以使用信号量等待上一个功能完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM