gpt4 book ai didi

ios - 如何使用完成处理程序方法运行 for 循环?

转载 作者:行者123 更新时间:2023-11-30 11:31:44 25 4
gpt4 key购买 nike

我需要使用 DJI SDK 从无人机获取一些图像文件,但我完成的方式似乎不是最理想的。我想知道如何才能做到这一点,但在 for 循环内。代码如下:

func getImages(with file: DJIMediaFile?) {
guard let file = file else { return }
file.fetchPreview { (error) in
if error != nil {
print(String(describing: error?.localizedDescription))
return
}

guard let image = file.preview else { print("No preview"); return }
self.images.append(image)
self.imageView.image = image
self.index += 1

if self.index < self.mediaList.count {
self.getImages(with: self.mediaList[self.index])
} else {
// [...]
}

}
}
}

熟悉 DJI SDK 和 Swift 的人将不胜感激(也许我应该使用其他 API 方法?)。谢谢!

最佳答案

这主要是一个快速问题,而不是 DJI SDK 问题,更多地与代码审查相关。但这只是简单的

func getNewImages() {
for file in self.mediaList where file != nil {
self.getImages(with: file!) { image, error in //not ideal to force unwarp but i don't know if the where clause supports if let binding, or if file is really nil, you'll have to check yourself in code
guard let newImage = image else { return }
self.cachedImages.append(newImage)
}
}

self.setImage(at: 0)
}

func getImages(with file: DJIMediaFile, completion: (UIImage?, Error?)->()) {
file.fetchPreview { (error) in // is this async? If its' async you may want to use dispatch groups in the for in loop to run the setImage at the correct time
if let newError = error {
print(String(describing: newError.localizedDescription))
completion(nil, error)
return
}

guard let image = file.preview else {
print("No preview")
completion(nil, nil)
return
}

completion(image, nil)
}
}

func setImage(at row: Int) {
guard row < self.cachedImages.count - 1 else { return }
self.imageView.image = self.cachedImages[row]
}

您的递归可能会导致您不希望出现的错误。这有点像您想要做的,但根据我在这里看到的内容应该很容易进行更改。

编辑:您可能还打算替换该文件,因为人们从无人机本身加载图像,在这种情况下,代码更像是这样:

func getNewImages() {
for file in self.mediaList where file != nil {
self.getImages(with: file!) { image, error in //not ideal to force unwarp but I don't know if the where clause supports if let binding, or if file is really nil, you'll have to check yourself in code
guard let newImage = image else { return }
self.imageView.image = newImage
}
}
}

func getImages(with file: DJIMediaFile, completion: (UIImage?, Error?)->()) {
file.fetchPreview { (error) in
if let newError = error {
print(String(describing: newError.localizedDescription))
completion(nil, error)
return
}

guard let image = file.preview else {
print("No preview")
completion(nil, nil)
return
}

completion(image, nil)
}
}

edit2:另请注意,这不一定是执行此操作的最佳方法,但以前是在 for 循环中执行此操作的方法。我认为更好的方法是在 mediaList 数组本身上使用 map 或 forEach ,如下所示。 (相同的 getImages 方法,没有任何变化)。这也可以在协议(protocol)、扩展等之间进一步细化,但在不了解您的程序的情况下,很难做出更好的建议。

func getNewImages() {
self.mediaList.forEach { file in
guard let newFile = file else { return }

self.getImages(with: newFile) { image, error in
guard let newImage = image else { return }
self.imageView.image = newImage
}
}
}

关于ios - 如何使用完成处理程序方法运行 for 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50184509/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com