Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImageView Extension for Downloading and Displaying Images in UICollectionView

I've Made and Extension of UIImageView that Allows me to Asynchronously Download images and then Assign them to their ImageView According to their specific Index (IndexPath in the CollectionView). It works great, the only problem is:

  • I STILL SEE WRONG IMAGES DISPLAYED WHEN SCROLLING FAST AND STOPPING.

Can anyone help me please? Here is my code:

// UIImageView Extension:

extension ExtendedUIImageView {

func downloadFrom(link link:String, contentMode mode: UIViewContentMode, imageIndex: Int) {

    if (NSFileManager.defaultManager().fileExistsAtPath(link)) {
        self.image = UIImage(contentsOfFile: link)
        return
    }

    self.imageURL = NSURL(string: link)
    let url = NSURL(string: link)
    contentMode = mode

    NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in
        guard
            let httpURLResponse = response as? NSHTTPURLResponse where httpURLResponse.statusCode == 200,
            let mimeType = response?.MIMEType where mimeType.hasPrefix("image"),
            let data = data where error == nil,
            let image = UIImage(data: data)
            else { return }
        dispatch_async(dispatch_get_main_queue()) { () -> Void in
            if (url == self.imageURL) {
                self.image = image
            }
        }
    }).resume()
  }
}

// And Id be Calling this method like this:

self.previewImageView?.downloadFrom(link: localPath, contentMode: .ScaleAspectFill, imageIndex: cell.indexPath.item)

// Where:

var previewImageView: ExtendedUIImageView?

PLEASE HELP ME :-O

like image 569
Hernan Arber Avatar asked Dec 07 '25 14:12

Hernan Arber


2 Answers

Hello~ I have been trying to do the same thing while ago. This is what I did. It was simple enough for me and working fine without any problems.

import Foundation
import UIKit

extension UIImageView {


    //load image async from inaternet
    func loadFromURL(photoUrl:String){
        //NSURL
        let url = NSURL(string: photoUrl)
        //Request
        let request = NSURLRequest(URL:url!);
        //Session
        let session = NSURLSession.sharedSession()
        //Data task
        let datatask = session.dataTaskWithRequest(request) { (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in
            if error != nil {
                print(error?.localizedDescription)
            }
            dispatch_async(dispatch_get_main_queue()) {
                self.image = UIImage(data: data!)
            }
        }
        datatask.resume()
    }


}
like image 191
stackflow Avatar answered Dec 10 '25 03:12

stackflow


with Kingfisher for caching

import Foundation
import UIKit
import Kingfisher

public extension UIImageView {
    func loadAvatar(url: URL) {
        self.kf.setImage(with: url, placeholder: UIImage(named: "default-avatar"), options: [.transition(.fade(0.2))])
    }

    func setImageWithIndicator(url: URL) {
        self.kf.indicatorType = .activity
        self.kf.setImage(with: url)
    }
}

like image 20
Cuyler Quint Avatar answered Dec 10 '25 04:12

Cuyler Quint