首页 > 解决方案 > DJI iOS SDK 以编程方式下载多个图像。首次下载后相机忙

问题描述

下面是我的实际代码。它总是很好地下载第一张图像,但给出“相机正忙或相机当前状态不支持该命令”。我尝试了如何使用 IOS DJI-SDK 以编程方式从无人机下载图像提出的解决方案

在过去的 10 天里,我一直在为此苦苦挣扎,并尝试了各种与异步队列等触发的组合,但没有成功。虽然 DJI 示例代码很多,但它们都专注于单次下载和预览等,而不是多文件下载的全部原始数据。非常感谢任何帮助。

func downLoad(){
     guard let drone = (DJISDKManager.product() as? DJIAircraft) else {
            testAlert(viewC: self, title: "Message", msg: "Unable to detect Drone", buttonText: "Dismiss")
            return
        }
        // Get camera on drone
        guard  let camera: DJICamera = drone.camera else {
            testAlert(viewC: self, title: "Message", msg: "Unable to detect Camera in initDownload()", buttonText: "Dismiss")
            return
        }
        // check if we can download images with the product
        if !camera.isMediaDownloadModeSupported() {
            testAlert(viewC: self, title: "Message", msg: "Product does not support media download mode", buttonText: "Dismiss")
            return
        }
        camera.setMode( .mediaDownload, withCompletion: {(error) in

            if error != nil {
                self.testAlert(viewC: self, title: "Message", msg: "Error \(error!.localizedDescription)", buttonText: "Dismiss")
            } 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
                    if error != nil {

首次下载后出现此错误

                        self.testAlert(viewC: self, title: "Message", msg: "Error refreshing list: \(error!.localizedDescription)", buttonText: "Dismiss")
                    }else {

                        // get list of files
                        guard let files = manager.sdCardFileListSnapshot() else {
                            self.testAlert(viewC: self, title: "Message", msg: "No files to download", buttonText: "Dismiss")
                            return
                        }
                        self.downloadImages(files: files, completion: { images in
                            //process images 
                           self.testAlert(viewC: self, title: "Message", msg: "Successfully downloaded all images and count \(images.count)", buttonText: "Dismiss")

                        })//end of downloadImages

                    }// end of else
                }) // end of file-refresh block
            } // end of if else
        })// end of camera setMode block
    }

    func downloadImages(files: [DJIMediaFile], completion: @escaping ([String]) -> Void){
        func downloadNextImage( files: [DJIMediaFile], index: Int = 0, downloadedFileUrls: [String] = []) {
            // stop when we reach the end of the list

            if (index >= files.count - 1)  {
                completion(downloadedFileUrls)
                return
            }
            else {
                var imageData = Data()
                var file = files[index]
                let isPhoto = file.mediaType == DJIMediaType.JPEG || file.mediaType == DJIMediaType.TIFF;

                var previousOffset: UInt = 0

                file.fetchData(withOffset: previousOffset, update: DispatchQueue.main, update: {(_ data: Data?, _ isComplete: Bool, _ error: Error?) -> Void in
                    if let error = error {
                        self.testAlert(viewC: self, title: "Error", msg: "File index : \(index) previousOffset : \(previousOffset) name: \(file.fileName) error : \(error)", buttonText: "Dismiss")
                    }
                    else {
                        imageData.append(data!)
                        previousOffset =  previousOffset + UInt((data?.count)!);
                        if isComplete {
                            if let image = UIImage(data: imageData) {
                                var imageUrl : String = ""
                                if(isPhoto){
                                    imageUrl = saveFile(imag)
                                }else //video
                                {
                                    //process video
                                }
                                downloadNextImage( files: files, index: (index + 1), downloadedFileUrls: downloadedFileUrls + [imageUrl])
                            }// If image
                        } //If complete
                    }// no eroor
                }) // end of filedata fetch
            }   // end of else statement
        }
        // start the recursive function
        downloadNextImage( files: files, index: 0)
    }

标签: swiftdownloadcameradji-sdk

解决方案


推荐阅读