Hernan Arber
Hernan Arber

Reputation: 1636

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:

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

Upvotes: 3

Views: 4683

Answers (2)

Cuyler Quint
Cuyler Quint

Reputation: 196

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)
    }
}

Upvotes: 2

stackflow
stackflow

Reputation: 2122

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()
    }


}

Upvotes: 5

Related Questions