0

I have the following code inside cellForRowAt method to fetch image and load into the cell's imageView. The image is confirmed to be downloaded from the print statements, but image is not showing in the cell. Please help me.

let request = URLRequest(url: URL(string: imageURL)!)

        let session = URLSession.shared
        let dataTask = session.dataTask(with: request as URLRequest) {(data, response, error) in
            // The download has finished.
            if let e = error {
                print("Error downloading picture: \(e)")
            } else {
                // No errors found.
                // It would be weird if we didn't have a response, so check for that too.
                if let res = response as? HTTPURLResponse {
                    print("Downloaded image with response code \(res.statusCode)")
                    if let imageData = data {
                        // Finally convert that Data into an image and do what you wish with it.
                        let image = UIImage(data: imageData)
                        // Do something with your image.

                        DispatchQueue.main.async {
                            cell.imgView.contentMode = .scaleAspectFit
                            cell.imgView.image = image
                        }

                        print("image received")
                    } else { print("Couldn't get image: Image is nil") }
                } else { print("Couldn't get response code for some reason") }
            }
        }
        dataTask.resume()
as diu
  • 1,010
  • 15
  • 31
  • Did you check if `cell.imgView` is not `nil` when you set image to it? – Taras Chernyshenko Oct 05 '17 at 13:19
  • @SalmanGhumsani I want to use native libraries provided by Apple instead of AlamoFire. Thank you for responding. – as diu Oct 05 '17 at 13:21
  • Have you stepped through in debug to see what the value of `cell` is? and `cell.imgView`? are they valid? – DonMag Oct 05 '17 at 13:24
  • Check that `image` is non-nil. (You check `imageData` but not the image itself.) – Phillip Mills Oct 05 '17 at 13:24
  • @TarasChernyshenko I checked if not nil and still image is not loading. Thank you for responding. – as diu Oct 05 '17 at 13:32
  • @asdiu so it is `nil` or not? I didn't get it – Taras Chernyshenko Oct 05 '17 at 13:37
  • @TarasChernyshenko it is not nil. There is a value assigned to it. – as diu Oct 05 '17 at 13:38
  • 1
    @asdiu - Since you want to avoid 3rd-party libraries, you may want to take the approach of using a `UIImageView` extension, instead of your code block (which I assume is inside `cellForRowAt`?). This can be used for any image view, not just inside a table cell: https://stackoverflow.com/a/39339641/6257435 – DonMag Oct 05 '17 at 13:46
  • 1
    First question: there’s no chance you’ve forgotten to add imgView as a subviews to your cell is there? Second question: have you tried storing the image as a parameter to your cell subclass? If you do this you can check what value is being passed to the cell via a didSet observer, and also assign the image to the image view and call setNeedsDisplay when the value of image changes. – Marcus Oct 05 '17 at 13:47
  • @DonMag with that solution, the image will be downloaded every time if I call that code from `cellForRowAt`. So I'm updating a local variable for image and storing it there. this image is being fetched from another. method from within the same viewController. – as diu Oct 05 '17 at 14:06
  • @Sparky I confirmed that the imgView is linked to the `UIImageView` outlet on the cell. Secondly, I'm storing the image in a variable within the same class. I will try to debug using the didSet method you suggested and get back thank you. – as diu Oct 05 '17 at 14:08

1 Answers1

0

I think you just missing a reload of your cell's subviews (imageView) to show the image. You also probably want to keep track of the right cell.

cell.tag = indexPath.row //after you init the cell

DispatchQueue.main.async {
   if cell.tag == indexPath.row
   cell.imgView.contentMode = .scaleAspectFit
   cell.imgView.image = image
   cell.setNeedsLayout()
 }
zero3nna
  • 2,770
  • 30
  • 28