错误下载图片

时间:2017-02-02 19:26:07

标签: ios json swift image url

我是Swift的新手,我使用ObjectMapper解析我的JSON,但我希望数据在TableView中显示。但是我的下载图片有问题 我使用扩展UIImageView func downloadFrom

我的问题:

  

无效重新声明'download From(url:ContentMode :)'

我的代码:

        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CategoryTableViewCell", for: indexPath) as! CategoryTableViewCell

        let strUrl = categoty[indexPath.row].picture

        cell.titleCategory.text = self.categoty[indexPath.row].title

        cell.imageCategory.downloadFrom(url: URL(string: strUrl!)!)

        return cell
    }
}

extension UIImageView {
    func downloadFrom(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)
                else { return }
            DispatchQueue.main.async() { () -> Void in
                self.image = image
            }
            }.resume()
    }
}

enter image description here

1 个答案:

答案 0 :(得分:0)

错误意味着您已在代码中两次使用相同名称编写功能相同的函数。

在代码中查找此方法的重复实现:

func downloadFrom(url: URL, contentMode mode: UIViewContentMode = .scaleAspectFit) {