gpt4 book ai didi

iOS - 由于内存问题错误而终止

转载 作者:行者123 更新时间:2023-11-28 05:49:56 26 4
gpt4 key购买 nike

所以我正在进行网络调用以检索图像并使用它来展示一种视频。过了一会儿,我可以看到内存丧失和能量影响: enter image description here

一段时间后,我的应用崩溃了,我得到:“由于内存问题错误而终止”

在此之前,我从图像调用方法中得到了这个错误:“来自 dataResponse 的错误:操作无法完成。设备上没有剩余空间”

这是我使用的两种方法:

class InstallationViewController: BaseViewController {

func imageCaller(url: String , success: @escaping (UIImage) -> Void, failure: @escaping () -> Void) {
let handler = AuthenticateHandler()
self.urlSession = URLSession(configuration: URLSessionConfiguration.default, delegate: handler, delegateQueue: OperationQueue.main)

self.imageThumbnailTask = urlSession?.dataTask(with: URL(string:url)!) { data, res, err in

if err != nil {
print("error from dataResponse:\(err?.localizedDescription ?? "Response Error")")
failure()
return
}
DispatchQueue.main.async {

if let imageData = data, let image = UIImage(data: imageData) {
success(image)
URLCache.shared.removeAllCachedResponses()

}
}
}
self.imageThumbnailTask?.resume()
}

func imageThumbnailcall() {
self.indicaotrTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.HandleOverTime), userInfo: nil, repeats: false)

self.imageCaller( url: self.isShowingThermal ? self.thermalUrl : self.visualUrl, success: { (image) in

self.indicaotrTimer?.invalidate()

DispatchQueue.main.async{
self.imageLoaderIndicator.stopAnimating()
self.backGroundImageView.image = image
}
if self.isInVC {
self.imageThumbnailcall()
}
}) {
self.imageLoaderIndicator.stopAnimating()
}
}

值得一提的是这一行:

   let handler = AuthenticateHandler()
self.urlSession = URLSession(configuration: URLSessionConfiguration.default, delegate: handler, delegateQueue: OperationQueue.main)

摘要协议(protocol)

最佳答案

看起来您在 imageThumbnailcall 函数的闭包中有一个保留周期。闭包创建了对 self 的强引用,并且由于它是一个递归函数,您将很快耗尽内存。您需要在闭包中将 self 捕获为 [weak self][unowned self]。使用 [unowned self] 的示例:

func imageThumbnailcall() {
self.indicaotrTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.HandleOverTime), userInfo: nil, repeats: false)

self.imageCaller( url: self.isShowingThermal ? self.thermalUrl : self.visualUrl, success: { [unowned self] (image) in

self.indicaotrTimer?.invalidate()

DispatchQueue.main.async{
self.imageLoaderIndicator.stopAnimating()
self.backGroundImageView.image = image
}
if self.isInVC {
self.imageThumbnailcall()
}
}) {
self.imageLoaderIndicator.stopAnimating()
}
}

如果你想了解更多

关于iOS - 由于内存问题错误而终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53299624/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com