swift - Swift 中的网址图片

标签 swift uicollectionview swift3

我使用的是 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/

相关文章:

uicollectionview - iOS 11 : UICollectionView: when appearing, 滚动发生

swift - "of"在 swift 3 中的函数参数之前

ios - Xcode 8 结束关闭自动完成问题

ios - 如何获取打开的球形摄像头设备的IP地址并检查是否已连接?

ios - collectionView didSelectItemAt indexPath 未调用 swift

swift - 在 Google map 上显示多个 'GMSMarker' 标记

ios - UICollectionViewFlowLayout 子类不起作用?

swift 3.0 : What is the neatest way to tackle this scenario with enums?

swift - 如何使用 alamofire 将服务的响应存储在模型中

swift - CGAffineTransform 恢复旋转方向