所以我在与Swift的努力中遇到了另一个障碍.我正在尝试将多个图像加载到图像库中 - 除了一件事以外,一切正常.尽管我清除了图像,但应用程序的内存使用仍在不断增长和增长.在基本上消除了所有代码之后,我发现这是由我的图像加载脚本引起的:
func loadImageWithIndex(index: Int) {
let imageURL = promotions[index].imageURL
let url = NSURL(string: imageURL)!
let urlSession = NSURLSession.sharedSession()
let query = urlSession.downloadTaskWithURL(url, completionHandler: { location, response, error -> Void in
})
query.resume()
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,这段代码基本上没有任何功能.然而,每次我调用它,我的应用程序内存使用量都在增长.如果我注释掉查询,则内存使用量不会改变.
我已经阅读了几个类似的问题,但他们都涉及使用委托.那么,在这种情况下,没有委托,但存在内存问题.有人知道如何消除它以及导致它的原因吗?
编辑:这是一个完整的测试类.似乎只有在可以加载图像时内存才会增长,就像指向图像的指针永远保存在内存中一样.当找不到图像时,没有任何反应,内存使用率保持低水平.也许有些暗示如何清理这些指针?
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
//memory usage: approx. 23MB with 1 load according to the debug navigator
//loadImage()
//memory usage approx 130MB with the cycle below according to the debug navigator
for i in 1...50 {
loadImage() …Run Code Online (Sandbox Code Playgroud)