swift - DJI iOS SDK 以编程方式下载多个图像。首次下载后相机忙
问题描述
下面是我的实际代码。它总是很好地下载第一张图像,但给出“相机正忙或相机当前状态不支持该命令”。我尝试了如何使用 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)
}
解决方案
推荐阅读
- python - 如何在 Python 中创建评分系统?
- perl - 这是一种有效的插入排序形式吗?
- python-3.x - H2OConnectionError: 意外的 HTTP 错误: HTTPConnectionPool(host='localhost', port=54321):
- java - 如何在没有任何主类的情况下在 netbeans IDE 中运行 Java 小程序
- php - 通过php将数据保存到postgres
- c++ - cpp项目中.fdf和.h文件的区别
- java - 防止在 webView 中使用键盘
- python - 蟒蛇 | 合并一个文件夹中的多个 csv (100+) 文件,并考虑 csv 标头
- php - 尝试将数据插入数据库表但没有任何反应?
- c# - 如何在 asp.net core 2.0 中上传大文件