我使用的是 LJWebImage 示例项目,它是我在以下链接的 github 上找到的 https://github.com/likumb/LJWebImage .项目,类似于 SDWebImage。我成功地将示例项目导入到我的项目中。我正在使用 collectionView 从包含 url 链接数量的 plist 中填充图像。我正在尝试将 collectionView 选择的 cellimage 传递给我的 imageView 但没有运气.请有人指出我的方向。下面是我的部分 collectionView 代码。
提前致谢。
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
}
}
}
最佳答案
您可以使用 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
}
关于swift - Swift 中的网址图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39676329/