DJI iOS SDK以编程方式下载多个图像。相机首次下载后忙碌

时间:2018-10-12 17:07:43

标签: swift download camera dji-sdk

这是我下面的实际代码。它总是下载完好的第一张图像,但提示“相机正忙或相机当前状态不支持该命令”。我尝试了How to programmatically download Images from drone using the 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)
    }

0 个答案:

没有答案