2

I want to asynchronously dispatch 8 image urls in a collection view. I have created a class for collection view cell and also made an outlet to imageview in it. Now I want to configure the imageview from main view controller. Here is the code

    let reuseIdentifier = "PhotosCollectionViewCell" // also enter this string as the cell identifier in the storyboard
   var items = ["1", "2", "3", "4", "5", "6", "7", "8"]

    // tell the collection view how many cells to make
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       return self.items.count

    }

    // make a cell for each cell index path
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        // get a reference to our storyboard cell
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! PhotosCollectionViewCell


        cell.imageView = imageView

        return cell



    }

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        // handle tap events
        print("You selected cell #\(indexPath.item)!")
    }



    func loadImage() {
        dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0)) {
            let urlString = "http://charcoaldesign.co.uk/AsyncImageView/Forest/IMG_0352.JPG"
            let url = NSURL(string: urlString)
            let data = NSData(contentsOfURL: url!)
            dispatch_async(dispatch_get_main_queue(), {
             self.imageView.image = UIImage(data: data!)
//              self.items[0] = (data as? String)!

            })

        }
    }
}
Isabel Inc
  • 1,871
  • 2
  • 21
  • 28
Venky
  • 125
  • 9

2 Answers2

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

    // get a reference to our storyboard cell
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! PhotosCollectionViewCell


    cell.imageView = imageView
    loadImage(imageView, urlPAssed: urlString)
    return cell
}
     func loadImage(imageViewObj : UIImageView , urlPAssed : NSString) {

    //let urlString = "http://charcoaldesign.co.uk/AsyncImageView/Forest/IMG_0352.JPG"
    let url = NSURL(string: urlPAssed as String)
    NSURLSession.sharedSession().dataTaskWithURL(url!) { (data, response, error) in
        if(error==nil)
        {
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                imageViewObj.image = UIImage(data: data!)

            })
        }
        else
        {
            imageViewObj.image = UIImage(named: "dummy")
        }
        }.resume()
     }
good4pc
  • 711
  • 4
  • 17
  • Thank you for the reply .here i am having 8 image urls how can i run a loop,so that i can display 8 images in a collection view-good4pc – Venky Aug 22 '16 at 12:05
  • pass your imageurl along with the imageView like this, so it will be called dynamically according to your collectionViewCell. loadImage(imageView, url) , func loadImage(imageViewObj : UIImageView , url : NSString) – good4pc Aug 22 '16 at 12:18
  • I am getting lot of errors please give me a detailed solution – Venky Aug 22 '16 at 12:43
0

Here is a extension to make things more easy.

extension UIImageView {
    func downloadImageFrom(link link:String, contentMode: UIViewContentMode) {
        NSURLSession.sharedSession().dataTaskWithURL( NSURL(string:link)!, completionHandler: {
            (data, response, error) -> Void in
            dispatch_async(dispatch_get_main_queue()) {
                self.contentMode =  contentMode
                if let data = data { self.image = UIImage(data: data) }
            }
        }).resume()
    }
}

cell.imageView.downloadImageFrom(link: imageLinkArray[indexPath.row], contentMode: UIViewContentMode.ScaleAspectFit)  //set your image from link array.

Also you can look below url for more help.
how to implement lazy loading of images in table view using swift

Community
  • 1
  • 1
Nik
  • 1,679
  • 1
  • 20
  • 36