从url获取图像后出现失败url错误



我想从URL中获取图像到我的TableView中。我在UIImageView上创建了扩展,这样我就可以下载图像:

extension UIImageView {
func downloaded(from url: URL, contentMode mode: UIView.ContentMode = .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() { [weak self] in
self?.image = image
}
}.resume()
}
func downloaded(from link: String, contentMode mode: UIView.ContentMode = .scaleAspectFit) {
guard let url = URL(string: "https://image.tmdb.org/t/p/original(link)") else { return }
print(url)
downloaded(from: url, contentMode: mode)

}
}

在第二个下载的函数中,我为参数添加了一个URL值,因为JSON对象只包含URL的一半,所以我需要将这个前缀添加到完全打开的链接中。

在我的TableViewCell文件中,我使用imageUrl参数制作了configureCell函数,该参数是JSON对象的半url。

func configureCell(songName: String, songName2: String, imageUrl: String) {
songNameLabel.text = songName
songNameLabel2.text = songName2
if let url = URL(string: imageUrl) {
artistImageView.downloaded(from: url)
}
}

在ViewController中的cellForRowAt函数中,我添加了以下代码

let song = movieList[indexPath.row]

cropCell.configureCell(songName: song.title, songName2: song.overview, imageUrl: song.backdropPath)

功能是配置标签很好,但对于图像,我得到了这个错误

任务<2>完成时出现错误[-1002]错误域=NSURLError域代码=-1002";不支持的URL";UserInfo={NSLocalizedDescription=不支持的URL,NSErrorFailingURLStringKey=/620hnMVLu6RSZW6a5rwO8gqpt0t.jpg,NSErrorFailingURLKey=/6200hnMVLu6SZW6a5rwO8g qpt0t..jpg,_NSURLErrorRelatedURLSessionTaskErrorKey=("LocalDataTask<2>quot;)

其仅打开没有添加前缀I的backropPath URl。

JSON对象:

"results":[{"adult":false,"backdrop_path":"/620hnMVLu6RSZW6a5rwO8gqpt0t.jpg","genre_ids":[16,35,10751,14],"id":508943,"original_language":"en","original_title":"Luca","overview":"Luca and his best friend Alberto experience an unforgettable summer on the Italian Riviera. But all the fun is threatened by a deeply-held secret: they are sea monsters from another world just below the water’s surface.","popularity":7586.545,"poster_path":"/jTswp6KyDYKtvC52GbHagrZbGvD.jpg","release_date":"2021-06-17","title":"Luca","video":false,"vote_average":8.2,"vote_count":1250}

我哪里错了?

您从configureCell方法调用downloaded(from url: URL...)方法,而不是从downloaded(from link: String...)调用

最新更新