在已设置的图像 swift 4 上加载图像的问题 [英] problem with images loading on top of already set images swift 4

查看:22
本文介绍了在已设置的图像 swift 4 上加载图像的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 cellForItemAtIndexPath 中遇到问题,我将图像设置为单元格的 UIButton,但每次滚动 collectionView 的单元格时,它都会一次又一次地将图像放置在已设置的图像之上.我可以说是因为图像的阴影越来越厚.我正在从该快速文件中创建的图像文字数组中提取图像,并且正在加载正确的图像,因此那里没有问题.我确信这对大多数人来说是一个简单的解决方法,但我似乎无法在任何地方找到答案.

I'm having an issue in my cellForItemAtIndexPath where I am setting an image to my cell's UIButton but every time I scroll the collectionView's cells, it's placing the image on top of the already set image again and again. I can tell because the shadow of the image is getting thicker and thicker. I'm pulling the images from an array that I created of image literals in that swift file and the correct images are loading so there's no problem there. I'm sure this is a simple fix for most but I can't seem to find an answer anywhere.

我的 cellForItemAtIndexPath 函数的图像

我的应用在我滚动之前运行

滚动一下后的应用程序

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! HomeViewCell

    collectionView.bounces = false

    let imageNumber = indexPath.item

    let collectionImage: UIButton = {
        let image = UIButton()
        image.setImage(collectionImageArray[imageNumber].withRenderingMode(.alwaysOriginal), for: .normal)
        image.contentMode = .scaleAspectFit
        image.addTarget(self, action: #selector(handleCollectionTap), for: .touchUpInside)
        return image
    }()

    collectionImage.imageView?.image = collectionImageArray[imageNumber]

    cell.addSubview(collectionImage)
    collectionImage.anchor(top: nil, left: nil, bottom: nil, right: nil, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)
    collectionImage.centerXAnchor.constraint(equalTo: cell.centerXAnchor).isActive = true
    collectionImage.centerYAnchor.constraint(equalTo: cell.centerYAnchor).isActive = true


    print(imageNumber)

    return cell
}

推荐答案

如果其他人遇到这个问题(或者也许我是唯一一个愚蠢的人),问题是我应该创建我的 UIButton,添加子视图,并将其约束在单元格类中,并从 cellForItem AtindexPath 方法中设置图像和目标处理程序,如下所示.

If anyone else comes across this issue (or maybe I'm the only dumb one), the problem was that I should have created my UIButton, added the subview, and constrained it inside of the cell class and from the cellForItem AtindexPath method set the image and target handler like this.

class HomeViewCell: UICollectionViewCell {

let collectionImage: UIButton = {
    let image = UIButton(type: .custom)
    image.contentMode = .scaleAspectFit
    return image
}()

override init(frame: CGRect) {
    super.init(frame: frame)

    addSubview(collectionImage)
    collectionImage.anchor(top: nil, left: nil, bottom: nil, right: nil, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)
    collectionImage.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
    collectionImage.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! HomeViewCell

    collectionView.bounces = false

    let imageNumber = indexPath.item

    let image = collectionImageArray[imageNumber].withRenderingMode(.alwaysOriginal)
    cell.collectionImage.setImage(image, for: .normal)
    cell.collectionImage.addTarget(self, action: #selector(handleCollectionTap), for: .touchUpInside)

    print(imageNumber)

    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: view.frame.width, height: view.frame.height)
}

@objc func handleCollectionTap() {
    let layout = UICollectionViewFlowLayout()
    let cardViewer = CardViewerController(collectionViewLayout: layout)
    present(cardViewer, animated: true, completion: nil)
}

现在一切都很顺利!:)

Everything is running smoothly now! :)

这篇关于在已设置的图像 swift 4 上加载图像的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆