通过 AlamofireImage 下载 UIImage? [英] Downloading UIImage via AlamofireImage?
本文介绍了通过 AlamofireImage 下载 UIImage?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 URL,想通过返回函数下载图像,但是我不能让它正常协作,这是我的函数:
I have a URL and want to download the image via a return function, however I cant get it to cooperate properly, here is my func:
func getTabImage(url: URL) -> UIImage {
Alamofire.request(url)
.responseImage { response in
if let image = response.result.value {
return image
} else {
print("Failed to get image")
}
}
}
我传入了 URL,并希望从 alamofire 响应返回一个 UIImage.
I pass in the URL, and want a UIImage returned from the alamofire response.
但是我明白了
void 函数中出现意外的非 void 返回值
Unexpected non-void return value in void function
用于返回语句.
我怎样才能正确地做到这一点?
How can i achieve this correctly?
推荐答案
您可以使用以下功能下载图片:
You can use the below function for downloading the image:
func getImage(_ url:String,handler: @escaping (UIImage?)->Void) {
print(url)
Alamofire.request(url, method: .get).responseImage { response in
if let data = response.result.value {
handler(data)
} else {
handler(nil)
}
}
}
用途
getImage("http://") { (image) in
if image != nil {
print(image)
}
}
或
如果您想在 UIImageView
上设置图像,请使用 AlamofireImage 的扩展名.
If you want to set the image on UIImageView
use extension of AlamofireImage.
if let imageURL = URL(string: "http://"), let placeholder = UIImage(named: "default") {
imageView.af_setImage(withURL: imageURL, placeholderImage: placeholder) //set image automatically when download compelete.
}
这篇关于通过 AlamofireImage 下载 UIImage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文