UICollectionViewCells无法正确显示

时间:2017-06-25 21:57:46

标签: ios uiimageview uicollectionview uicollectionviewcell phasset

我正在创建一个应用,您可以为自己的帐户创建个人资料照片。当然,我不得不将照片选择器作为获取照片的一种选择。起初我打算使用UIImagePickerController,但后来我读到了Swift 3“Photos”模块。我编写了整个选择器屏幕的大部分,但我仍然有显示问题。而不是将照片很好地放置,每行3个,就像我编写的那样,当我运行应用程序时,视图看起来像这样:

Picker view

显然,这不是我想要的视图。这是我的单元格生成方法的代码。

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)

    var assetThumbnailSize = CGSize(width: self.view.frame.width/3, height: self.view.frame.width/3)

    let asset = assets[indexPath.row]

    PHImageManager.default().requestImage(for: asset, targetSize: assetThumbnailSize, contentMode: .aspectFill, options: nil, resultHandler: {(result, info)in
        if let image = result {
            let imageView = UIImageView()
            imageView.image = image
            cell.addSubview(imageView)
            imageView.bounds = cell.bounds
        }
    })

    return cell
}

我还将单元格大小与imageView大小相同。以下是该方法的代码:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    return CGSize(width: self.view.frame.width/3, height: self.view.frame.width/3)
}

我认为它在图像之间显示巨大空间的原因是因为我在界面构建器中设置它的方式,我认为这并不重要,因为我以编程方式对其进行了修改:

Interface builder

所以基本上我认为界面构建器设置正在推翻程序设置。如果有人对如何解决这个问题有任何建议,我将不胜感激!

0 个答案:

没有答案
相关问题