Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 来自调试器的消息:在带有gif图像的UITableView/UICollectionView翠鸟库中滚动时,由于内存问题而终止_Ios_Swift_Uitableview_Uicollectionview_Kingfisher - Fatal编程技术网

Ios 来自调试器的消息:在带有gif图像的UITableView/UICollectionView翠鸟库中滚动时,由于内存问题而终止

Ios 来自调试器的消息:在带有gif图像的UITableView/UICollectionView翠鸟库中滚动时,由于内存问题而终止,ios,swift,uitableview,uicollectionview,kingfisher,Ios,Swift,Uitableview,Uicollectionview,Kingfisher,代码段: func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell: BroadCastFeedTableViewCell = broadCastTableView.dequeueReusableCell(withIdentifier: "broadCastFeedCell") as BroadCastFeedTableViewC

代码段:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell: BroadCastFeedTableViewCell = broadCastTableView.dequeueReusableCell(withIdentifier: "broadCastFeedCell") as BroadCastFeedTableViewCell

    cell.singlePicImageView.kf.setImage(with: URL(string: (self.broadCastArray[indexPath.row].images_url?[0])!), placeholder: nil, options: nil, progressBlock: nil) 
            { (theImage, error, cache, url) in
                if theImage != nil{
                    let imageHeight = self.getAspectRatioOfDownloadedImages(resolution: self.broadCastArray[indexPath.row].image_resolution![0])
                    cell.collectionContentViewHeightConstraint.constant = imageHeight

                    cell.layoutSubviews()
                    cell.layoutIfNeeded()

                }else {
                    let placeHolderCenterCordi = UIView().getViewRelationWithSuperSuperView(viewControllerView: cell.collectionContentView, 
                                                                    subView: cell.singlePicImageView, subObjFrame: cell.singlePicImageView.frame)

                    cell.singlePicImageView.addPlaceHolderImageView(cordinates: placeHolderCenterCordi.center)
                }
               self.broadCastTableView.rowHeight = UITableViewAutomaticDimension
            }
}
在上面的代码中,我使用翠鸟库从远程服务器加载图像,每当下面的代码加载某些图像(GIF、JPEG、PNG),这些图像可能有较大的大小(大约2-5 MB),应用程序就会因内存问题而终止。在iphone5s中,它会在应用程序启动后立即终止,而在其他iPhone型号(7、6s)中,它会在滚动一定时间后终止。我也检查了分配和泄漏,但我不太了解/发现这个问题

我还附上了剖面图。这表明没有此类内存泄漏,但由于某些问题,应用程序正在终止:


我认为在您的代码中,您使用了“Self”作为强约束,因此它创建了一个强引用循环,因为这会导致内存泄漏。因此,此时您可以尝试使用[weakself],然后运行代码

cell.singlePicImageView.kf.setImage(with: URL(string: (self.broadCastArray[indexPath.row].images_url?[0])!), placeholder: nil, options: nil, progressBlock: nil) 
        { [weak self] (theImage, error, cache, url) in
            if theImage != nil{
                let imageHeight = self?.getAspectRatioOfDownloadedImages(resolution: self?.broadCastArray[indexPath.row].image_resolution![0])
                cell.collectionContentViewHeightConstraint.constant = imageHeight

                cell.layoutSubviews()
                cell.layoutIfNeeded()

            }else {
                let placeHolderCenterCordi = UIView().getViewRelationWithSuperSuperView(viewControllerView: cell.collectionContentView, 
                                                                subView: cell.singlePicImageView, subObjFrame: cell.singlePicImageView.frame)

                cell.singlePicImageView.addPlaceHolderImageView(cordinates: placeHolderCenterCordi.center)
            }
           self?.broadCastTableView.rowHeight = UITableViewAutomaticDimension
        }

我在加载图像时遇到了很多问题。我不能肯定你的情况,但我可以给你一些有用的提示

  • 查看UITableViewDataSourceRefetch。这是一种在单元格中渲染图像之前下载图像的方法
  • 考虑在某个地方创建一个助手函数来启动下载并允许取消下载。例如,当您快速向下滚动tableview时,每个单元格都将开始下载图像,即使图像未显示。如果手机没了,你可以取消下载
  • 单元格基本上应该是代码中的UI元素。其思想是,尝试将网络请求从单元本身中取出,并确保单元仅包含要渲染的元素(图像、标签、视图),单元具有func configure(withModel:Model)函数,并且在cellForRow上,您只需将数据传递给它进行渲染。单元格是重复使用和频繁更新的,您不应该在其上执行大量操作。请记住,当一个单元格被加载时,它将调用这些函数,然后它可以离开屏幕并再次返回,它将重新执行所有这些函数
  • 让cell类执行正确的布局更新和刷新,并尽量不要在cellForRow中执行。您所能做的只是确保imageview获得图像,但不依赖于执行单元格更改,并且此图像下载是异步的,下载结束时,单元格甚至可能不在那里
  • 缓存。您可以缓存这些图像,还是它们会不断变化?确保您了解KF是如何缓存您的图像的,这正是您所需要的缓存。如果没有,则进行调整
  • [吹毛求疵]调用
    cell.layoutSubviews()
    ,然后调用
    cell.layoutifneed()
    。第一个是强制调用layoutSubView,第二个是在调用layoutSubView之前检查是否有需要布局的内容。如果要标记要布局的视图,可以执行setNeedsLayout()。这将使视图布局在下一个周期更新

  • 回答我自己的问题

    下面的内容对我有用

    我在翠鸟图书馆提供的UIImageView中使用了animateImageView类

    let imageView = AnimatedImageView()
    imageView.kf.setImage(with: URL(string: "your_image"), placeholder: nil, options: [.targetCache(ImageCache(name: "your_cache_name")), .backgroundDecode], progressBlock: nil) { (image, error, cache, url) in 
      // some code
    }
    
    注意重要事项

    [.targetCache(ImageCache(名称:“您的缓存名称”),.backgroundDecode]


    在上面的选项属性代码中,我使用了我自己的缓存(ImageCache(名称:“your\u cache\u name”),并要求翠鸟解码背景中的图像。将有助于没有兄弟没有内存泄漏,检查附加图像的剖析