Swift 使用SDWebImage加载GIF占用的内存远远超过预期
我正在构建一个UITableView,其中包含使用SDWebImage下载的图像和GIF。图像显示在表格单元格中,而GIF显示为3D触摸预览 问题是,每次预览GIF时,内存都会出现峰值(正如预期的那样),但内存使用量的增加远远超过了我的预期。手动下载其中一个GIF时,我发现它只有500kB左右,但通过SDWebImage在应用程序中执行此操作时,设置内存增加接近100MB 只需几次GIF预览,应用程序就会因内存问题而崩溃 起初,我认为当GIF预览退出时,GIFView的实例可能没有被释放-我用“deinit”检查了一下,它看起来确实在释放 我还将ViewDidEnglishe中的GIFView重置为“nil”,但这也没有帮助 查看分配工具,我发现GIF在第一次加载时被永久存储在内存中。这解释了为什么再次查看相同的GIF不会增加内存 有人知道为什么这些GIF会占用这么多内存吗?我还构建了一个应用程序版本,其中所有资产都是本地的,运行平稳,没有打嗝或内存问题,但两种情况下GIF大小肯定应该相同 如果没有泄漏,并且这些内存峰值不可避免,那么当GIF消失时,有没有其他方法可以清除内存中的GIF 代码如下Swift 使用SDWebImage加载GIF占用的内存远远超过预期,swift,memory,memory-leaks,sdwebimage,Swift,Memory,Memory Leaks,Sdwebimage,我正在构建一个UITableView,其中包含使用SDWebImage下载的图像和GIF。图像显示在表格单元格中,而GIF显示为3D触摸预览 问题是,每次预览GIF时,内存都会出现峰值(正如预期的那样),但内存使用量的增加远远超过了我的预期。手动下载其中一个GIF时,我发现它只有500kB左右,但通过SDWebImage在应用程序中执行此操作时,设置内存增加接近100MB 只需几次GIF预览,应用程序就会因内存问题而崩溃 起初,我认为当GIF预览退出时,GIFView的实例可能没有被释放-我用“
//model
struct Visualisation: Codable {
var id: Int
let name, info, url_name, tags, imageURL, gifURL: String
}
//3D touch GIF preview
extension ViewController: UIViewControllerPreviewingDelegate {
func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
guard let indexPath = tableView.indexPathForRow(at: location) else { return nil }
let selectedVis = filteredVisualisations[indexPath.row]
let identifier = "GIFViewController"
guard let GIFVC = storyboard?.instantiateViewController(withIdentifier: identifier) as? GIFViewController else { return nil}
GIFVC.selection = selectedVis
GIFVC.preferredContentSize = CGSize(width: 0, height: 190)
return GIFVC
}
//GIFViewController
import UIKit
import SDWebImage
class GIFViewController: UIViewController {
@IBOutlet weak var GIFView: UIImageView!
var selection = visualisations[0]
override func viewDidLoad() {
super.viewDidLoad()
GIFView.sd_setImage(with: URL(string: selection.gifURL))
// Do any additional setup after loading the view.
}
override func viewDidDisappear(_ animated: Bool = true) {
}
deinit {
print("DEALLOCATED GIFViewController class")
}
}
//setting the table cells
class TableViewCell: UITableViewCell {
@IBOutlet weak var ImageView: UIImageView!
@IBOutlet weak var TitleLabel: UILabel!
@IBOutlet weak var InfoLabel: UILabel!
func setCell(visualisation: Visualisation){
ImageView.sd_setImage(with: URL(string: visualisation.imageURL))
TitleLabel.text = visualisation.name
InfoLabel.text = visualisation.info
}
}
任何帮助都将不胜感激
另外,我相信你已经注意到我对iOS开发非常陌生。。。请原谅我的“新手”…我通过在视图消失时清除图像缓存来解决问题,但这可能是一个有点钝锤的解决方案…我通过在视图消失时清除图像缓存来解决问题,但这可能是一个有点钝锤的解决方案。。。