Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alamofire-image loading wrong Image inside uicollectionview cell

i have been using alamofire-image library to display image from url inside uicollectionview cell.

i have noticed that most of time its displays the right image but sometimes displays wrong image(most of the time previous cell image). how to overcome this problem.

      func collectionView(collectionView: UICollectionView,
        cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

          let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell",forIndexPath: indexPath) as! myCollectionViewCell

          cell.titleLabelCell.text = element[indexPath.row]["title"]
          cell.generLabelCell.text  = element[indexPath.row]["gener"]

          let URL = NSURL(string: "\(element[indexPath.row]["banner_site_url"])" )!

          cell.mImageView.af_setImageWithURL(URL)

          return cell
      }
like image 681
Ajay Singh Thakur Avatar asked Sep 26 '22 03:09

Ajay Singh Thakur


1 Answers

You have to break the connection with any image download request that may be in progress when you reuse the cell.

You can find a good tutorial written by Todd Kramer at:

Downloading, Caching, & Decoding Images Asynchronously In Swift With Alamofire: Part 1

Note the implementation of the cell view class PhotoCollectionViewCell. It calls the member function reset, which cancels any existing request, before launching a request for the current image.

If the images are small and likely to be reused I tend to cancel the cell update rather than the retrieval.

Read the whole thing.

like image 177
Bob Wakefield Avatar answered Oct 04 '22 15:10

Bob Wakefield