通过 AlamofireImage 下载 UIImage?

编程入门 行业动态 更新时间:2024-10-28 20:22:44
本文介绍了通过 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("") { (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: ""), let placeholder = UIImage(named: "default") { imageView.af_setImage(withURL: imageURL, placeholderImage: placeholder) //set image automatically when download compelete. }

更多推荐

通过 AlamofireImage 下载 UIImage?

本文发布于:2023-11-26 14:26:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634130.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:AlamofireImage   UIImage

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!