【问题标题】:Swift 4 Error: "Cannot convert value of type 'URLRequest' to expected argument type 'URL!'"Swift 4 错误:“无法将 'URLRequest' 类型的值转换为预期的参数类型 'URL!'”
【发布时间】:2018-03-16 09:11:21
【问题描述】:

正如标题所说的那样。我收到了这个错误,不确定这是否与 Swift 4 转换有关。我一直在尝试解决代码,但还没有运气,所以决定把这个问题放在 SO 上。在线抛出此错误:“cell.businessPostImage.setImageWith(postRequest,”。谢谢。

cell.businessPostImage.image = nil
        if let postURL = URL(string: downloadURL) {
            let postRequest = URLRequest(url: postURL)
            cell.businessPostImage.setImageWith(postRequest, placeholderImage: nil, options:
            { (imageRequest, imageResponse, image) in
                    cell.businessPostImage.contentMode = UIViewContentMode.scaleToFill
                    cell.businessPostImage.image = image
            }, completed: { (imageRequest, imageResponse, error) -> Void in
                // failure downloading image
                print("Error downloading Firebase post image")
                print(error)
            })
        }

【问题讨论】:

  • 传入postURL 而不是postRequest
  • 请检查我刚刚添加的图片,因为编辑而引发了新错误。
  • 您正在尝试将闭包传递给options 参数。
  • 我明白了,我应该重新格式化这段代码吗?或者有解决方案吗?

标签: ios swift image url nsurlrequest


【解决方案1】:

你必须像这样使用:

if let postURL = URL(string: downloadURL) {
    let postRequest = URLRequest(url: postURL)
    cell.businessPostImage.setImageWith(postURL, placeholderImage: nil, options: SDWebImageOptions.ProgressiveDownload, completed: { (imageRequest, imageResponse, error) -> Void in
        // failure downloading image
        print("Error downloading Firebase post image")
        print(error)
    })
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-03
    • 2017-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多