在CollectionView单元格运动中加载UIImage冻结

时间:2019-05-25 12:42:05

标签: swift uitableview uicollectionview

我有一个带有1至3个UIImageView的UICollectionViewCell,这些UIImageView由stackView管理。

图像全部约8mb。我先从CoreData异步加载它们,然后再调用collectionView.reload()

在此设置中,在加载新单元格时移动时,收集视图会冻结。

我尝试使用较小的图片进行尝试,然后没有冻结。

我现在按比例缩小图像,然后再将其加载到UIImageView中。但这会导致其他奇怪的行为,例如首先显示其他单元格中的图片,然后添加正确的图片。

override func configureCellAtIndexPath(item: Journaling) {
        super.configureCellAtIndexPath(item: item)

        let imageOne = UIImageView()
        let imageTwo = UIImageView()
        let imageThree = UIImageView()
        imageContainer = nil
        imageOne.image = nil
        imageTwo.image = nil
        imageThree.image = nil


        if item.image.count == 1 {
            let group = DispatchGroup()
            group.enter()
            var downsizedImage = [UIImage]()
            DispatchQueue.global().async {
                downsizedImage.append(item.image[0].downsizeImage(.low)!)

                group.leave()
            }

            group.notify(queue: .main) {
                imageOne.image = downsizedImage[0]
                imageTwo.image = nil
                imageThree.image = nil
                self.setUpStackView([imageOne])
            }
        }

        if item.image.count == 2 {
            let group = DispatchGroup()
            group.enter()
            var downsizedImage = [UIImage]()
            DispatchQueue.global().async {
                downsizedImage.append(item.image[0].downsizeImage(.low)!)
                downsizedImage.append(item.image[1].downsizeImage(.low)!)
                group.leave()
            }

            group.notify(queue: .main) {
                imageOne.image = downsizedImage[0]
                imageTwo.image = downsizedImage[1]
                imageThree.image = nil
                self.setUpStackView([imageOne,imageTwo])
            }


        }

        if item.image.count > 2 {
            let group = DispatchGroup()
            group.enter()
            var downsizedImage = [UIImage]()
            DispatchQueue.global().async {
                downsizedImage.append(item.image[0].downsizeImage(.low)!)
                downsizedImage.append(item.image[1].downsizeImage(.low)!)
                downsizedImage.append(item.image[2].downsizeImage(.low)!)

                group.leave()
            }

            group.notify(queue: .main) {
                imageOne.image = downsizedImage[0]
                imageTwo.image = downsizedImage[1]
                imageThree.image = downsizedImage[2]
                self.setUpStackView([imageOne,imageTwo,imageThree])
            }
        }

        imageOne.contentMode = .scaleAspectFill
        imageOne.clipsToBounds = true
        imageTwo.contentMode = .scaleAspectFill
        imageTwo.clipsToBounds = true
        imageThree.contentMode = .scaleAspectFill
        imageThree.clipsToBounds = true
    }

    func setUpStackView(_ views: [UIView]) {
        self.imageContainer = UIStackView(arrangedSubviews: views)
        self.imageContainer.axis = .horizontal
        self.imageContainer.distribution = .fillEqually

        placeholder.sv(imageContainer)
        imageContainer.addConstrainsToSuperView(distance: 0, superView: placeholder)

    }

0 个答案:

没有答案