简体   繁体   中英

SDK DJI download photo in swift

I am simply attempting to download the latest access the feed from my DJI drone and convert the feed into a UIImage. The easiest way i thought this could be done is take a photo, then promptly afterwards download the latest photo from the storage. I will add the download from storage code below. Is this the easiest way? I have been receiving this error code -

Settings parameters operation failed. (Code : -1007)

I've researched but couldn't find what this meant - The DJI docs are limited to pretty much all obj-c. Here the code -

@IBAction func download(_ sender: UIButton) {
    let camera = self.fetchCamera()

    // switch camera mode to allow for media downloads
    camera?.setMode(DJICameraMode.mediaDownload, withCompletion: {(error) in
        if error != nil {
            self.status.text="\(error!.localizedDescription)"
        }
        else {

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

                if error != nil {
                    self.status.text = "State: \(manager.sdCardFileListState.rawValue)"
                    self.status.text = "Error refreshing list: \(error)"

                }
                else {
                    print("Refreshed file list")
                    print("State: \(manager.sdCardFileListState.rawValue)")


                    guard let files = manager.sdCardFileListSnapshot() else {
                        self.status.text = ("No files to download")
                        return
                    }

                    self.status.text = "There are files to download"

                    var images: [UIImage] = []

                    for file in files {

                        if file.mediaType == .JPEG {

                            self.status.text = ("Time created: \(file.timeCreated)")

                            DispatchQueue.main.asyncAfter(deadline: .now() + 1) {

                                file.fetchData(withOffset: 0, update: DispatchQueue.main, update: {(_ data: Data?, _ isComplete: Bool, _ error: Error?) -> Void in

                                    if error != nil {
                                        self.status.text = "State: \(manager.sdCardFileListState.rawValue)"
                                        self.status.text = "Error downloading photo: \(error!)"
                                    }
                                    else {
                                        // unwrap downloaded data and create image
                                        if let data = data, let downloadedImage = UIImage(data: data) {
                                            self.status.text = "Image was downloaded!"
                                            images.append( downloadedImage )
                                        }
                                    }

                                }) // end of filedata fetch

                            }

                        }

                    } // end of loop


                }

        }

    })// end of camera setMode block

}` 

Any help would be greatly appreciated - Thanks

@IBAction func download(_ sender: UIButton) {
    let camera = self.fetchCamera()

// switch camera mode to allow for media downloads
camera?.setMode(DJICameraMode.mediaDownload, withCompletion: {(error) in
    if error != nil {
        self.status.text="\(error!.localizedDescription)"
    }
    else {

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

            if error != nil {
                self.status.text = "State: \(manager.sdCardFileListState.rawValue)"
                self.status.text = "Error refreshing list: \(error)"

            }
            else {
                print("Refreshed file list")
                print("State: \(manager.sdCardFileListState.rawValue)")

                while(manager.internalStorageFileListState != .UpToDate) {
                    // Do nothing...
                }
                guard let files = manager.sdCardFileListSnapshot() else {
                    self.status.text = ("No files to download")
                    return
                }

                self.status.text = "There are files to download"

                var images: [UIImage] = []

                for file in files {

                    if file.mediaType == .JPEG {

                        self.status.text = ("Time created: \(file.timeCreated)")

                        DispatchQueue.main.asyncAfter(deadline: .now() + 1) {

                            file.fetchData(withOffset: 0, update: DispatchQueue.main, update: {(_ data: Data?, _ isComplete: Bool, _ error: Error?) -> Void in

                                if error != nil {
                                    self.status.text = "State: \(manager.sdCardFileListState.rawValue)"
                                    self.status.text = "Error downloading photo: \(error!)"
                                }
                                else {
                                    // unwrap downloaded data and create image
                                    if let data = data, let downloadedImage = UIImage(data: data) {
                                        self.status.text = "Image was downloaded!"
                                        images.append( downloadedImage )
                                    }
                                }

                            }) // end of filedata fetch
                        }
                    }

                } // end of loop
            }
    }

})// end of camera setMode block
}

Why dont you override the callback for a "new file generated" and then download the file there

func camera(_ camera: DJICamera, didGenerateNewMediaFile newMedia: DJIMediaFile) {
     newMedia.fetchData(withOffset: 0, update: DispatchQueue.main, update: {(_ data: Data?, _ isComplete: Bool, _ error: Error?) -> Void in

                            if error != nil {
                                self.status.text = "State: \(manager.sdCardFileListState.rawValue)"
                                self.status.text = "Error downloading photo: \(error!)"
                            }
                            else {
                                // unwrap downloaded data and create image
                                if let data = data, let downloadedImage = UIImage(data: data) {
                                    self.status.text = "Image was downloaded!"
                                    images.append( downloadedImage )
                                }
                            }

                        }) // end of filedata fetch


}

You will have to implement the "DJICameraDelegate" class and set the delegate in your ViewController. Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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