DJI iOS SDK downloading multiple images programati

2019-09-06 03:03发布

问题:

Here is my actual code below. It always downloads the first image fine, but gives "Camera is busy or the command is not supported in the camera's current state". I tried with the solution proposed by How to programmatically download Images from drone using the IOS DJI-SDK

I have been struggling with this for the past 10 days and tried all sorts of combination with triggering in async queue etc. but no success. Though DJI samples have a lot of code, they all focus on single download and preview etc. but not the entire raw data with multiple file downloads. Greatly appreciate any help.

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 {

Error here after first download

                        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)
    }