Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var cameraSystemState: DJICameraSystemState?
- var isPlayback: Bool = false
- // My download function
- func downloadMediaPhoto(iDs: [String]) {
- print("iDs count: \(iDs.count)")
- let camera = self.fetchCamera()
- camera?.delegate = self
- var images: [Data] = []
- var imageData = Data()
- // 0. show hub
- showDownloadingHub()
- // 1. switch to playback mode
- camera?.setMode(.mediaDownload, withCompletion: { [unowned self] (error) in
- guard error == nil else {
- print("3. \(String(describing: error))")
- Alerts.showAlert("3. \(String(describing: error?.localizedDescription))", completion: nil)
- self.gotoUpLoadingVcIfNeeded()
- return
- }
- var countDownTimeOut = 0
- repeat {
- sleep(1)
- countDownTimeOut += 1
- print("Waiting playback mode")
- } while (self.isPlayback == false && countDownTimeOut < 60)
- if countDownTimeOut >= 60 {
- Alerts.showAlert("Disconnected to Aircraft!", completion: nil)
- self.hideDownloadingHub()
- camera?.setMode(.shootPhoto, withCompletion: nil)
- self.gotoUpLoadingVcIfNeeded()
- }
- runOnGlobalThread {
- let downloadGroup = DispatchGroup()
- // 3. check selected file
- for (index, id) in iDs.enumerated() {
- var isDownloadFile = true
- // 4. downloadSelectedFile
- camera?.playbackManager?.downloadSelectedFiles(preparation: { (fileName, fileType, fileSize, skip) in
- print("fileName: \(String(describing: fileName)) | fileType: \(fileType) | fileSize: \(fileSize) | skip: \(skip)")
- imageData = Data()
- }, process: { (data, _) in
- if let data = data {
- imageData.append(data)
- }
- }, fileCompletion: {
- images.append(imageData)
- DispatchQueue.global(qos: DispatchQoS.QoSClass.default).async {
- downloadGroup.enter()
- self.saveImage(imageData: imageData, id: id, fullRes: true)
- downloadGroup.leave()
- }
- }, overallCompletion: { (error) in
- print("4. \(String(describing: error))")
- if error != nil {
- Alerts.showAlert("4. \(String(describing: error?.localizedDescription))", completion: nil)
- }
- if index < iDs.count - 1 {
- camera?.playbackManager?.goToPreviousSinglePreviewPage()
- isDownloadFile = false
- } else if index == iDs.count - 1 {
- isDownloadFile = false
- }
- })
- repeat {
- sleep(1)
- } while (isDownloadFile)
- }
- downloadGroup.notify(queue: DispatchQueue.main) {
- self.hideDownloadingHub()
- camera?.setMode(.shootPhoto, withCompletion: nil)
- self.gotoUpLoadingVcIfNeeded()
- }
- }
- })
- }
- // MARK: - DJICameraDelegate
- extension StreamingVC: DJICameraDelegate {
- func camera(_ camera: DJICamera, didUpdate systemState: DJICameraSystemState) {
- self.cameraSystemState = systemState
- isPlayback = (systemState.mode == .playback) || (systemState.mode == .mediaDownload)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement