如何在单元格中调用图像URL和图像视图以获取集合视图

时间:2016-08-22 11:50:54

标签: ios swift

我想在集合视图中异步调度8个图像网址。我已经为集合视图单元创建了一个类,并且还在其中创建了一个imageview的出口。现在我想从主视图控制器配置imageview。这是代码

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

            })

        }
    }
}

2 个答案:

答案 0 :(得分: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()
     }

答案 1 :(得分:0)

这是一个让事情变得更容易的扩展。

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.

另外,您可以在下方查看网址以获取更多帮助 how to implement lazy loading of images in table view using swift