【问题标题】:Swift3: How to get images in UICollectionView?Swift3:如何在 UICollectionView 中获取图像?
【发布时间】:2017-06-17 17:08:58
【问题描述】:

我是 swift3 的初学者,在从 UICollectionView 中获取图像时遇到问题。我按照本教程(https://github.com/zhangao0086/DKImagePickerController)从照片库上传多张图片,但我不知道如何从 UICollectionView 中检索图片。

来自 ViewController 的代码

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let asset = self.assets![indexPath.row]
    var cell: UICollectionViewCell?
    var imageView: UIImageView?

    if asset.isVideo {
        cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CellVideo", for: indexPath)
        imageView = cell?.contentView.viewWithTag(1) as? UIImageView
    } else {
        cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CellImage", for: indexPath)
        imageView = cell?.contentView.viewWithTag(1) as? UIImageView
    }

    if let cell = cell, let imageView = imageView {
        let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
        let tag = indexPath.row + 1
        cell.tag = tag
        asset.fetchImageWithSize(layout.itemSize.toPixel(), completeBlock: { image, info in
            if cell.tag == tag {
                imageView.image = image
            }
        })
    }

    return cell!
}

非常感谢任何帮助。

非常感谢!

【问题讨论】:

  • 你到底想要什么?

标签: ios image swift3 uicollectionview


【解决方案1】:

根据您的要求,您可以执行以下操作以从 collectionView 获取图像。

var imagesArray = [UIImage]()
for i in (0...collectionView.numberOfItems(inSection: 0)) {
        let indexPath = IndexPath.init(row: i, section: 0)
        let cell = collectionView(collectionView, cellForItemAt: indexPath)
        let imageView = cell.viewWithTag(11) as! UIImageView
        let image = imageView.image
        imagesArray.append(image!)
}

我建议您从资源库中获取图像。因为这行代码let cell = collectionView(collectionView, cellForItemAt: indexPath) 实际上做了同样的事情。

【讨论】:

  • 哦,我明白了...如何从资源库中获取图像?
猜你喜欢
  • 2018-07-11
  • 1970-01-01
  • 1970-01-01
  • 2018-06-01
  • 1970-01-01
  • 2017-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多