2017-02-01 1 views
0

私はswift3の初心者で、UICollectionViewから画像を取得する際に問題が発生しています。私は写真ライブラリから複数の画像をアップロードする際にこのチュートリアル(https://github.com/zhangao0086/DKImagePickerController)に従っていましたが、UICollectionViewから画像を取り出す方法はわかりません。Swift3:UICollectionViewで画像を取得するには?

Screenshot of the 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! 
} 

からのコードすべてのヘルプははるかに高く評価されます。

ありがとうございました!

+0

実際に何をしたいですか? –

答えて

0

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)は事実上同じことをします。

+0

ああ、私は...資産ライブラリから画像を取得するにはどうすればいいですか? – codingStudent

関連する問題