如何从 HTTP 请求将图像加载到 UIButton Swift

Posted

技术标签:

【中文标题】如何从 HTTP 请求将图像加载到 UIButton Swift【英文标题】:How to load Image into UIButton Swift From HTTP Request 【发布时间】:2018-07-04 23:59:24 【问题描述】:

我一直从我的 API 中获取图像,过去我已经将它们加载到 UIImage 中,您将在下面看到扩展名。但是,现在我正在尝试从 API 获取图像并将它们加载到 UIButton 图像视图中。我不知道如何处理扩展和其他代码以使其工作。感谢您的帮助!

扩展

Extension UIImageView 
func getURL2(url: URL, contentMode mode: UIViewContentMode = .scaleAspectFit) 
    contentMode = mode
    URLSession.shared.dataTask(with: url)  data, response, error in
        guard
            let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200,
            let mimeType = response?.mimeType, mimeType.hasPrefix("image"),
            let data = data, error == nil,
            let image = UIImage(data: data),
            httpURLResponse.url == url
            else  return 
        DispatchQueue.main.async() 
            self.image = image
        
        .resume()


func downloadedFrom2(link: String, contentMode mode: UIViewContentMode = .scaleAspectFit) 
    guard let url = URL(string: link) else  return 
    getURL2(url: url, contentMode: mode)

    

其他代码

func loadProfilePhoto(image: UIButton, link: String) 
    image.downloadedFrom2(link: link)
    image.imageView!.clipsToBounds = true
    image.imageView!.layer.cornerRadius = (image.imageView!.frame.height) / 2
    image.imageView!.contentMode = .scaleAspectFill


func loadRandom8() 
    if self.users.count == 8 
        let completelink1 = users[0].picture_url
        //ex. https://api.adorable.io/avatars/200/AngelicAlling.png
        let completelink2 = users[1].picture_url
        let completelink3 = users[2].picture_url
        let completelink4 = users[3].picture_url
        let completelink5 = users[4].picture_url
        let completelink6 = users[5].picture_url
        let completelink7 = users[6].picture_url
        let completelink8 = users[7].picture_url

        loadProfilePhoto(image: p2Image, link: completelink1)
        loadProfilePhoto(image: p2Image, link: completelink2)
        loadProfilePhoto(image: p3Image, link: completelink3)
        loadProfilePhoto(image: p4Image, link: completelink4)
        loadProfilePhoto(image: p5Image, link: completelink5)
        loadProfilePhoto(image: p6Image, link: completelink6)
        loadProfilePhoto(image: p7Image, link: completelink7)
        loadProfilePhoto(image: p8Image, link: completelink8)

【问题讨论】:

使用第三方库,如 KingFisher 【参考方案1】:

您当前的扩展是 UIImageView,但您想在 UIButton 中加载图像,因此将扩展更改为 UIButton 并确保按钮的类型设置为自定义而不是系统。您可以从情节提要中执行此操作。

extension UIButton 
  func getURL2(url: URL, contentMode mode: UIViewContentMode = .scaleAspectFit) 
    contentMode = mode
    URLSession.shared.dataTask(with: url)  data, response, error in
      guard
        let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200,
        let mimeType = response?.mimeType, mimeType.hasPrefix("image"),
        let data = data, error == nil,
        let image = UIImage(data: data),
        httpURLResponse.url == url
        else  return 
      DispatchQueue.main.async() 
        self.setImage(image, for: .normal)
        self.imageView?.contentMode = .scaleAspectFit
        //self.image = image
      
      .resume()
  

  public func downloadedFrom2(link: String, contentMode mode: UIViewContentMode = .scaleAspectFit) 
    guard let url = URL(string: link) else  return 
    getURL2(url: url, contentMode: mode)

  

【讨论】:

感谢您的帮助。我现在就试试。 不错的解决方案,需要更新这一行:- (self.imageView?.contentMode = .scaleAspectFit) 到 (self.imageView?.contentMode = mode)

以上是关于如何从 HTTP 请求将图像加载到 UIButton Swift的主要内容,如果未能解决你的问题,请参考以下文章

如何从post async http请求下载数据时将数据加载到reclyerview中

当我提出新请求时,Ajax http 请求将以前的图像加载到轮播中

如何将图像文件传递到 Flutter 中的 Http 请求(POST)正文?

如何将图像视图从改造加载的片段传递给子片段?

如何从android中重定向url加载图像

如何在没有新服务器请求的情况下将下载的图像从父路由传递到子路由