Sanja Kaloki
Sanja Kaloki

Reputation: 37

getting failing url error after fetching image from url

I want to fetch image from URL into my TableView. I create extension on UIImageView so I can download image:

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)
    
}
}

In second downloaded function I added a URL value to parameter because JSON object contains only half of URL, so I needed to add this prefix to fully open link.

In my TableViewCell file I made configureCell function with imageUrl parameter which is half url from JSON Object.

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

In cellForRowAt function in ViewController I added this code

let song = movieList[indexPath.row]
    
cropCell.configureCell(songName: song.title, songName2: song.overview, imageUrl: song.backdropPath)

Function is configuring labels well, but for image I am getting this error

Task .<2> finished with error [-1002] Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo={NSLocalizedDescription=unsupported URL, NSErrorFailingURLStringKey=/620hnMVLu6RSZW6a5rwO8gqpt0t.jpg, NSErrorFailingURLKey=/620hnMVLu6RSZW6a5rwO8gqpt0t.jpg, _NSURLErrorRelatedURLSessionTaskErrorKey=( "LocalDataTask .<2>" )

which opens only backdropPath URl without prefix I added.

JSON Object:

"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}

Where am I getting wrong?

Upvotes: 1

Views: 81

Answers (1)

BoygeniusDexter
BoygeniusDexter

Reputation: 2242

you call downloaded(from url: URL...) method from the configureCell method, not the downloaded(from link: String...)

Upvotes: 1

Related Questions