ios - 在 Swift 中加载图像时应用程序卡住

标签 ios swift

我有这个数组:

media = [  
        [UIImage(named: "1.png")!,UIImage(named: "14.png")!,UIImage(named: "2.png")!],
        [UIImage(named: "3.png")!,UIImage(named: "15.png")!,UIImage(named: "4.png")!],
        [UIImage(named: "5.png")!,UIImage(named: "16.png")!,UIImage(named: "6.png")!],
        [UIImage(named: "7.png")!,UIImage(named: "17.png")!,UIImage(named: "8.png")!],
        [UIImage(named: "9.png")!,UIImage(named: "18.png")!,UIImage(named: "10.png")!],
        [UIImage(named: "11.png")!,UIImage(named: "19.png")!,UIImage(named: "12.png")!],
        [UIImage(named: "13.png")!,UIImage(named: "21.png")!,UIImage(named: "47.png")!]
    ]

我还有一个collectionView:

func numberOfSections(in collectionView: UICollectionView) -> Int {
        return media.count
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return media[section].count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! MasterViewCell

        cell.thisImage.image = media[indexPath.section][indexPath.row]

        return cell
    }

但是我有一个问题。当我滚动 collectionView 时,我的应用程序卡住了。但是当我滚动所有部分并继续滚动下一个部分时。我的应用程序没有卡住。

如何解决?

最佳答案

为了更好地在 TableView/CollectionView 单元格上加载图像 SDWebImage是完美的图像加载库。将此添加到您的项目并用于在 collectionView 上加载图像。它将消除图像加载期间的 UI 卡住问题。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! MasterViewCell

        cell.thisImage.sd_setImage(with:Bundle.main.url(forResource: "\(indexPath.row + 1)", withExtension:"png"), placeholderImage: UIImage(named: "placeholder.png"))

        return cell
}

希望这会有所帮助。

关于ios - 在 Swift 中加载图像时应用程序卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51695926/

相关文章:

ios - UIButton 在整个表面上没有响应(UICollectionViewCell)

ios - 如何在 Swift 中使用 CoreText 替换 UILabel?

ios - 如何从 iOS 上的 Firebase 云消息传递获取 token

ios - 无法创建 IXPlaceholder Xcode 12.5

ios - 如何正确使用 UIElements 上的占位符 API

ios - 恢复购买 : Non-Consumable

swift - ld : framework not found Crashlytics Xcode7. 2.1

ios - 按频率列出数组中的对象,最常出现在最前面

iOS (Swift) - UIViewController 数组

iphone - 如何防止 iPhone 自动旋转