代码段
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)时,我都会使用Kingfisher库从远程服务器加载图像,这些图像可能较大(大约2-5 MB),该应用由于内存而终止问题。在iPhone 5s中,它会在启动应用程序后立即终止;而在其他iPhone模型(7、6s)中,它会在滚动一定时间后终止。我还检查了分配和泄漏,但是我对这个问题不了解/了解太多。
我还附上了分析图
Following Image show their is no such memory leaks, but due to some issue app is terminating
请帮助我解决以上问题。
答案 0 :(得分:1)
我在加载图像时遇到很多问题。我不确定您的情况,但可以给您一些有用的提示。
cell.layoutSubviews()
,然后致电cell.layoutIfNeeded()
。第一个是强制调用layoutSubviews,第二个是检查是否有需要在调用layoutSubView之前进行布局的内容。如果要标记要布局的视图,可以执行setNeedsLayout()。将在下一个周期更新视图布局。答案 1 :(得分:0)
我认为在您的代码中您将“ Self”用作强力限制,因此由于造成内存泄漏,因此它创建了强大的参考周期。因此,此时您可以尝试使用[弱自我],然后运行代码
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
}
答案 2 :(得分:0)
回答我自己的问题
以下为我工作
我在Kingfisher库提供的UIImageView中使用了AnimatedImageView类
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(name:“ your_cache_name”)),.backgroundDecode]
在上面的 options 属性代码中,我使用了自己的缓存(ImageCache(name:“ your_cache_name”)),并要求翠鸟在后台解码图像