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

时间:2018-07-04 23:59:24

标签: ios swift api uibutton uiimageview

我一直在从我的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)

1 个答案:

答案 0 :(得分:1)

您当前的扩展名是针对UIImageView的,但是您想将图像加载到UIButton中,因此将扩展名更改为UIButton并确保按钮的类型设置为Custom而不是System。您可以从情节提要中执行此操作。

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)

  }
}