功能不等待 Swift 中使用 Alamofire 下载图片
Function doesnt wait download Picture with Alamofire in Swift
我尝试使用 Alamofire 下载图片,此图片必须附加到自定义 class。但我没有那样做。图片是异步下载的,我没有解决这个问题。我的错误在哪里?
图像始终return无
// 标记:自定义函数
import UIKit
import Alamofire
import AlamofireImage
class DownloadPhotoWAlamofire {
func download(imageUid:String , completion : @escaping (UIImage) -> () ) {
let url = "\(Config.fileService)file?uid=\(String(describing: imageUid))"
print("URL \(url)")
Alamofire.request(url, method: .get ).responseImage { response in
print("Image Response \(response)")
let image = response.result.value
completion(image!)
}
}
}
// 标记:在 VIEWCONTROL 中
guard let objectElement = o as? [String:Any] else {return}
let managerName = objectElement["managerName"] as? String
let managerAvatarUid = objectElement["managerProfilePictureFileUid"] as? String
let assistantsOfDiariesUid = objectElement["assistantsOfDiariesUid"] as? String
var image:UIImage? = nil
if managerAvatarUid != nil {
DownloadPhotoWAlamofire().download(imageUid: managerAvatarUid!, completion: { (imageD) in
image = imageD
})
}
let e = AssistantInviteElement(managerName: managerName!, managerAvatarUid: managerAvatarUid, assistantsOfDiariesUid: assistantsOfDiariesUid!,avatarImage:image)
self.managerList.append(e)
将其更改为:
if managerAvatarUid != nil {
DownloadPhotoWAlamofire().download(imageUid: managerAvatarUid!, completion: { (imageD) in
let image = imageD
let e = AssistantInviteElement(managerName: managerName!, managerAvatarUid: managerAvatarUid, assistantsOfDiariesUid: assistantsOfDiariesUid!,avatarImage:image)
self.managerList.append(e)
})
}
由于下载是异步调用,您在下载图像之前执行了这一行:
let e = AssistantInviteElement(managerName: managerName!, managerAvatarUid: managerAvatarUid, assistantsOfDiariesUid: assistantsOfDiariesUid!,avatarImage:image)
当时图片为零。所以只有当你下载了图像并且它应该可以工作时才调用它。
也不要用“!”展开可选,做一个 if let/guard let。因此,将 Almofire 代码更改为:
Alamofire.request(url, method: .get ).responseImage { response in
print("Image Response \(response)")
if let image = response.result.value {
print("image downloaded: \(image)")
completion(image)
} else {
print("Image is nil")
}
}
启动异步下载后直接设置图片,当然还是nil
。在代码中使用回调的要点是,一旦回调被调用,您就只能使用请求的资源;所以你应该将使用图像的代码移到回调块中。
我尝试使用 Alamofire 下载图片,此图片必须附加到自定义 class。但我没有那样做。图片是异步下载的,我没有解决这个问题。我的错误在哪里?
图像始终return无
// 标记:自定义函数
import UIKit
import Alamofire
import AlamofireImage
class DownloadPhotoWAlamofire {
func download(imageUid:String , completion : @escaping (UIImage) -> () ) {
let url = "\(Config.fileService)file?uid=\(String(describing: imageUid))"
print("URL \(url)")
Alamofire.request(url, method: .get ).responseImage { response in
print("Image Response \(response)")
let image = response.result.value
completion(image!)
}
}
}
// 标记:在 VIEWCONTROL 中
guard let objectElement = o as? [String:Any] else {return}
let managerName = objectElement["managerName"] as? String
let managerAvatarUid = objectElement["managerProfilePictureFileUid"] as? String
let assistantsOfDiariesUid = objectElement["assistantsOfDiariesUid"] as? String
var image:UIImage? = nil
if managerAvatarUid != nil {
DownloadPhotoWAlamofire().download(imageUid: managerAvatarUid!, completion: { (imageD) in
image = imageD
})
}
let e = AssistantInviteElement(managerName: managerName!, managerAvatarUid: managerAvatarUid, assistantsOfDiariesUid: assistantsOfDiariesUid!,avatarImage:image)
self.managerList.append(e)
将其更改为:
if managerAvatarUid != nil {
DownloadPhotoWAlamofire().download(imageUid: managerAvatarUid!, completion: { (imageD) in
let image = imageD
let e = AssistantInviteElement(managerName: managerName!, managerAvatarUid: managerAvatarUid, assistantsOfDiariesUid: assistantsOfDiariesUid!,avatarImage:image)
self.managerList.append(e)
})
}
由于下载是异步调用,您在下载图像之前执行了这一行:
let e = AssistantInviteElement(managerName: managerName!, managerAvatarUid: managerAvatarUid, assistantsOfDiariesUid: assistantsOfDiariesUid!,avatarImage:image)
当时图片为零。所以只有当你下载了图像并且它应该可以工作时才调用它。
也不要用“!”展开可选,做一个 if let/guard let。因此,将 Almofire 代码更改为:
Alamofire.request(url, method: .get ).responseImage { response in
print("Image Response \(response)")
if let image = response.result.value {
print("image downloaded: \(image)")
completion(image)
} else {
print("Image is nil")
}
}
启动异步下载后直接设置图片,当然还是nil
。在代码中使用回调的要点是,一旦回调被调用,您就只能使用请求的资源;所以你应该将使用图像的代码移到回调块中。