1

i am getting images from url's and displaying it in tableview which happens successfully but when i scroll it automatically and repeatedly changes but after i pause at that point for a couple of seconds (roughly 10 or more which is a pretty long time) the proper image loads.

func load_image(urlString:String)
    {
        let imgURL: NSURL = NSURL(string: urlString)!
        let request: NSURLRequest = NSURLRequest(URL: imgURL)

        let session = NSURLSession.sharedSession()
        let task = session.dataTaskWithRequest(request){
            (data, response, error) -> Void in

            if (error == nil && data != nil)
            {
                func display_image()
                {
                    cell.pic.image = UIImage(data: data!)
                }

                dispatch_async(dispatch_get_main_queue(), display_image)
            }

        }

        task.resume()

    }
    load_image(urls[indexPath.row])
  • check this example it will help you http://stackoverflow.com/questions/16663618/async-image-loading-from-url-inside-a-uitableview-cell-image-changes-to-wrong – Muzahid Jul 31 '16 at 08:23
  • @Md.MuzahidulIslam thanks it helped, initializing cell.pic.image = nil stopped the fluctuation of images but when i scroll back up the image disappears and loads again but in a comparatively lesser amount of time – Kyle Fernandes Jul 31 '16 at 08:32

2 Answers2

0

First of all, why are You loading images in, I guess, cellForRowAtIndexPath? You can load it in viewDidLoad and store in some array. But if You need to do this as You are doing...

For better performance You can use NSCache to prevent loading images everytime tableview will show your cell. Try something like that:

let imageCache = NSCache()
func load_image(urlString:String)
{
    if let imageFromCache = imageCache.objectForKey(urlString) as? UIImage {
        cell.pic.image = imageFromCache
        return
    }
    let imgURL: NSURL = NSURL(string: urlString)!
    let request: NSURLRequest = NSURLRequest(URL: imgURL)

    let session = NSURLSession.sharedSession()
    let task = session.dataTaskWithRequest(request){
        (data, response, error) -> Void in

        if (error == nil && data != nil)
        {
            func display_image()
            {
                let imageToCache = UIImage(data: data!)
                cell.pic.image = UIImage(data: data!)
                imageCache.setObject(imageToCache!, forKey: urlString)
            }

            dispatch_async(dispatch_get_main_queue(), display_image)
        }

    }

    task.resume()

}

For more informations about NSCache you can check documentation.

eMKa
  • 201
  • 1
  • 7
  • i do load the images in cellForRowAt IndexPath but i retrieve the url from the database and store it in arrays in viewdidload. How would you recommend me doing it in viewDidLoad after retrieving the url itself ? I tried your suggested method, but images are still changing to one of the previous loaded images and changing back to the right one after few seconds. – Kyle Fernandes Aug 01 '16 at 15:09
  • Can You show Your code in `viewDidLoad` and `cellForRowAtIndexPath`? – eMKa Aug 02 '16 at 07:48
0

Your cell is being reused when scrolling. The cellForRowAt method will be called on scrolling.

Set imageView.image = nil in cellForRowAt method so no image will be display on the image view until they get downloaded.

We make imageView.image = nil is because if no image available for that cell then no image should be shown for that cell.

Jayraj Vala
  • 224
  • 2
  • 8