Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. var cameraSystemState: DJICameraSystemState?
  2. var isPlayback: Bool = false
  3.  
  4. // My download function
  5. func downloadMediaPhoto(iDs: [String]) {
  6. print("iDs count: \(iDs.count)")
  7. let camera = self.fetchCamera()
  8. camera?.delegate = self
  9. var images: [Data] = []
  10. var imageData = Data()
  11.  
  12. // 0. show hub
  13. showDownloadingHub()
  14.  
  15. // 1. switch to playback mode
  16. camera?.setMode(.mediaDownload, withCompletion: { [unowned self] (error) in
  17. guard error == nil else {
  18. print("3. \(String(describing: error))")
  19. Alerts.showAlert("3. \(String(describing: error?.localizedDescription))", completion: nil)
  20. self.gotoUpLoadingVcIfNeeded()
  21. return
  22. }
  23.  
  24. var countDownTimeOut = 0
  25. repeat {
  26. sleep(1)
  27. countDownTimeOut += 1
  28. print("Waiting playback mode")
  29. } while (self.isPlayback == false && countDownTimeOut < 60)
  30.  
  31. if countDownTimeOut >= 60 {
  32. Alerts.showAlert("Disconnected to Aircraft!", completion: nil)
  33. self.hideDownloadingHub()
  34. camera?.setMode(.shootPhoto, withCompletion: nil)
  35. self.gotoUpLoadingVcIfNeeded()
  36. }
  37.  
  38. runOnGlobalThread {
  39.  
  40. let downloadGroup = DispatchGroup()
  41.  
  42. // 3. check selected file
  43. for (index, id) in iDs.enumerated() {
  44. var isDownloadFile = true
  45.  
  46. // 4. downloadSelectedFile
  47. camera?.playbackManager?.downloadSelectedFiles(preparation: { (fileName, fileType, fileSize, skip) in
  48. print("fileName: \(String(describing: fileName)) | fileType: \(fileType) | fileSize: \(fileSize) | skip: \(skip)")
  49. imageData = Data()
  50.  
  51. }, process: { (data, _) in
  52. if let data = data {
  53. imageData.append(data)
  54. }
  55. }, fileCompletion: {
  56. images.append(imageData)
  57. DispatchQueue.global(qos: DispatchQoS.QoSClass.default).async {
  58. downloadGroup.enter()
  59. self.saveImage(imageData: imageData, id: id, fullRes: true)
  60. downloadGroup.leave()
  61. }
  62.  
  63. }, overallCompletion: { (error) in
  64. print("4. \(String(describing: error))")
  65. if error != nil {
  66. Alerts.showAlert("4. \(String(describing: error?.localizedDescription))", completion: nil)
  67. }
  68.  
  69. if index < iDs.count - 1 {
  70. camera?.playbackManager?.goToPreviousSinglePreviewPage()
  71. isDownloadFile = false
  72. } else if index == iDs.count - 1 {
  73. isDownloadFile = false
  74. }
  75. })
  76.  
  77. repeat {
  78. sleep(1)
  79. } while (isDownloadFile)
  80. }
  81.  
  82. downloadGroup.notify(queue: DispatchQueue.main) {
  83. self.hideDownloadingHub()
  84. camera?.setMode(.shootPhoto, withCompletion: nil)
  85. self.gotoUpLoadingVcIfNeeded()
  86. }
  87. }
  88. })
  89. }
  90.  
  91. // MARK: - DJICameraDelegate
  92. extension StreamingVC: DJICameraDelegate {
  93. func camera(_ camera: DJICamera, didUpdate systemState: DJICameraSystemState) {
  94. self.cameraSystemState = systemState
  95. isPlayback = (systemState.mode == .playback) || (systemState.mode == .mediaDownload)
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement