代码片段
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
}
}
在上面的代码中,我使用Kingfisher库从远程服务器加载图像,每当下面的代码加载某些图像(GIF,JPEG,PNG)时,可能有大尺寸(约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
请帮我解决上述问题。
我在内存加载图像方面遇到了很多问题。我不能确定你的情况,但我可以给你一些有用的提示。
cell.layoutSubviews()
然后cell.layoutIfNeeded()
。第一个是强制调用layoutSubviews,第二个是在调用layoutSubView之前检查是否有需要布局的东西。如果要标记要布局的视图,可以执行setNeedsLayout()。这将使视图布局在下一个周期更新。我认为在你的代码中你使用“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
}
回答我自己的问题
以下为我工作
我为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]
在上面的选项代码属性中,我使用了自己的缓存(ImageCache(名称:“your_cache_name”))并要求翠鸟在背景中解码图像