2016-09-24 24 views
1

我使用LJWebImage示例項目,其中,我發現從github上的以下鏈接https://github.com/likumb/LJWebImage。項目,類似於SDWebImage.I成功導入示例項目到我的項目中。我正在使用collectionView填充plist中包含多個url鏈接的圖像。我試圖將collectionView選中的cellimage傳遞給我的imageView,但沒有運氣,因此對於。請有人指點我的方向。我的部分collectionView代碼如下。在Swift中的URL圖像

在此先感謝。

let apps: NSArray = AppInfo.appInfo() 

@IBOutlet weak var imageView: UIImageView! 

@IBOutlet weak var collectionView: UICollectionView! 

override func viewDidLoad() { 
    super.viewDidLoad() 

      collectionView!.reloadData() 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 

} 

// MARK: - CollectionView data source 


func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 

    return apps.count 
} 


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 


    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath as IndexPath) as! Cell 

    cell.app = apps[indexPath.row] as? AppInfo 

    return cell 
} 

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 

    if let filePath = Bundle.main.path(forResource: "apps", ofType: "plist"), 
     let image = UIImage(contentsOfFile: filePath) { 
     imageView.contentMode = .scaleAspectFit 
     imageView.image = image 
    } 

    } 

} 
+0

請顯示準備從第一個vc到第二個vc的繼續或導航的代碼。 –

+0

@Nirav D collectionView和imageView在相同的viewController.i我沒有使用segue ..感謝 – Joe

+0

然後訪問plist而不是應用程序數組是什麼意思。 –

回答

2

您可以使用indexPath從數據源數組中檢索相應的應用程序對象。

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 

    guard let app = apps[indexPath.row] as? AppInfo else { return } 
    guard let image = UIImage(contentsOfFile: app.filePath) else { return } 

    imageView.contentMode = .scaleAspectFit 
    imageView.image = image 
} 

或者您可以使用indexPath檢索單元格,並從它的imageView中獲取圖像。

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 

    guard let cell = collectionView.cellForItem(at: indexPath) as? Cell else { return } 

    imageView.contentMode = .scaleAspectFit 
    imageView.image = cell.imageView.image 
} 
+0

你的indexPath方法就像一個魅力.....謝謝 – Joe