【问题标题】:How to call image urls and image view in cell for index path function for a collection view如何在单元格中调用图像 url 和图像视图以获取集合视图的索引路径功能
【发布时间】:2016-08-22 11:50:54
【问题描述】:

我想在一个集合视图中异步调度 8 个图像 url。我为集合视图单元创建了一个类,并在其中创建了一个 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)!

            })

        }
    }
}

【问题讨论】:

  • 在集合视图中使用 UIImageView+WebCache 异步图像

标签: ios swift


【解决方案1】:

这是一个让事情变得更简单的扩展。

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

【讨论】:

    【解决方案2】:
    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()
         }
    

    【讨论】:

    • 感谢您的回复。这里我有 8 个图像 url 我如何运行一个循环,以便我可以在集合视图中显示 8 个图像-good4pc
    • 像这样将您的 imageurl 与 imageView 一起传递,因此它将根据您的 collectionViewCell 动态调用。 loadImage(imageView, url) , func loadImage(imageViewObj : UIImageView , url : NSString)
    • 我遇到很多错误请给我详细的解决方案
    猜你喜欢
    • 1970-01-01
    • 2012-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-08
    • 2016-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多