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
}
我在加载图像时遇到了很多问题。我不能肯定你的情况,但我可以给你一些有用的提示
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”),并要求翠鸟解码背景中的图像。将有助于没有兄弟没有内存泄漏,检查附加图像的剖析