1

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?

2
  • I don't understand. You are calling downloaded(from url:...) in configureCell, so you don't add the baseURL. Commented Jun 28, 2021 at 15:27
  • As I mentioned in the comments to your previous question delete [weak self] in. DispatchQueue closures are not @escaping, they don't cause retain cycles. Commented Jun 28, 2021 at 15:36

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.