2016-08-22 22 views
2

我想在集合視圖中異步分派8個圖像URL。我創建了一個集合視圖單元格的類,並在其中創建了一個imageview插件。現在我想從主視圖控制器配置imageview。下面是代碼如何在集合視圖中爲索引路徑函數調用圖像URL和圖像視圖

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

      }) 

     } 
    } 
} 
+0

使用的UIImageView +爲的WebCache在集合視圖 –

回答

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

異步圖像謝謝你。在這裏我有8頁圖片的URL如何運行一個循環的答覆,這樣我可以顯示在收集視圖good4pc 8張圖片 – Venky

+0

像這樣將imageurl與imageView一起傳遞,因此將根據您的collectionViewCell動態調用它。 loadImage(imageView,url),func loadImage(imageViewObj:UIImageView,url:NSString) – good4pc

+0

thanku我將試試 – Venky

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. 

此外,你可以看看下面的URL更多的幫助。
how to implement lazy loading of images in table view using swift

相關問題