【问题标题】:Error download images下载图片时出错
【发布时间】:2017-06-20 00:36:43
【问题描述】:

我是 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()
    }
}

【问题讨论】:

  • 这本质上意味着你已经声明了两个同名的函数..只需重命名你的函数

标签: ios json swift image url


【解决方案1】:

该错误表示您在代码中两次编写了同名函数相同的函数。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多