Ios 如何使用Swift关闭打开的文件?

Ios 如何使用Swift关闭打开的文件?,ios,swift,compiler-errors,storing-data,xcode9.1,Ios,Swift,Compiler Errors,Storing Data,Xcode9.1,我正在下载约1300张图片。这些都是小图像,总大小约为500KB。然而,在下载并将它们放入userDefault之后,我得到如下错误: libsystem\u network.dylib:nw\u route\u get\u ifindex::socket(PF\u route,SOCK\u RAW,PF\u route)失败:[24]打开的文件太多 假设,下载的png图像未关闭 我已经通过以下方式扩展了缓存大小: // Configuring max network request c

我正在下载约1300张图片。这些都是小图像,总大小约为500KB。然而,在下载并将它们放入userDefault之后,我得到如下错误:

libsystem\u network.dylib:nw\u route\u get\u ifindex::socket(PF\u route,SOCK\u RAW,PF\u route)失败:[24]打开的文件太多

假设,下载的png图像未关闭

我已经通过以下方式扩展了缓存大小:

    // Configuring max network request cache size
    let memoryCapacity = 30 * 1024 * 1024 // 30MB
    let diskCapacity = 30 * 1024 * 1024   // 30MB
    let urlCache = URLCache(memoryCapacity: memoryCapacity, diskCapacity: diskCapacity, diskPath: "myDiscPath")
    URLCache.shared = urlCache
这就是我存储图像的方法:

    func storeImages (){
        for i in stride(from: 0, to: Cur.count, by: 1) {
            // Saving into userDefault
            saveIconsToDefault(row: i)
        }
    }
我在将它们全部添加到userDefault后得到错误。所以,我知道他们在那里

编辑:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "CurrencyCell", for: indexPath) as? CurrencyCell else { return UITableViewCell() }

    if currencies.count > 0 {
        let noVal = currencies[indexPath.row].rank ?? "N/A"
        let nameVal = currencies[indexPath.row].name ?? "N/A"
        let priceVal = currencies[indexPath.row].price_usd ?? "N/A"

        getIcon(id: currencies[indexPath.row].id!, completion: { (retImg) in
            cell.configureCell(no: noVal, name: nameVal, price: priceVal, img: retImg)
        })
    }
    return cell
}
功能:

func getImageFromWeb(_ urlString: String, closure: @escaping (UIImage?) -> ()) {
    guard let url = URL(string: urlString) else {
        return closure(nil)
    }
    let task = URLSession(configuration: .default).dataTask(with: url) { (data, response, error) in
        guard error == nil else {
            print("error: \(String(describing: error))")
            return closure(nil)
        }
        guard response != nil else {
            print("no response")
            return closure(nil)
        }
        guard data != nil else {
            print("no data")
            return closure(nil)
        }
        DispatchQueue.main.async {
            closure(UIImage(data: data!))
        }
    }; task.resume()
}

func getIcon (id: String, completion: @escaping (UIImage) -> Void) {
    var icon = UIImage()

    let imageUrl = "https://files/static/img/\(id).png"

        getImageFromWeb(imageUrl) { (image) in
            if verifyUrl(urlString: imageUrl) == true {
                if let image = image {
                    icon = image
                    completion(icon)
                }
            } else {
                if let image = UIImage(named: "no_image_icon") {
                    icon = image
                    completion(icon)
                }
            }
        }
}
用法:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "CurrencyCell", for: indexPath) as? CurrencyCell else { return UITableViewCell() }

    if currencies.count > 0 {
        let noVal = currencies[indexPath.row].rank ?? "N/A"
        let nameVal = currencies[indexPath.row].name ?? "N/A"
        let priceVal = currencies[indexPath.row].price_usd ?? "N/A"

        getIcon(id: currencies[indexPath.row].id!, completion: { (retImg) in
            cell.configureCell(no: noVal, name: nameVal, price: priceVal, img: retImg)
        })
    }
    return cell
}
URLSession(配置:.default)
语法正在为每个请求创建一个新的
URLSession
。创建一个
URLSession
(将其保存在某个属性中),然后将其重新用于所有请求。或者,如果您确实没有对
URLSession
进行任何自定义配置,只需使用
URLSession.shared

let task = URLSession.shared.dataTask(with: url) { data, response, error in
    ...
}
task.resume()

您提到您正在
UserDefaults
中保存1300个图像。这不是存储那种类型的数据或那种数量的文件的正确位置。我建议您使用中概述的“缓存”文件夹


也不要试图将它们存储在“文档”文件夹中。有关更多信息,请参阅。

不要在
UserDefaults
中存储
UIImage
s。从来没有。尤其不是1300张图片。我感谢你的意见。然而,如果你也指出这些东西应该放在哪里,那不是更好吗?试试这个,保罗,谢谢你,但这些都是Obj-C,我没有经验。正如题名所示,我正在寻找快速解决方案。尽管如此,我认为我可以使用应用程序本地数据存储文件夹,例如“temp”文件夹。将继续研究。@Rob你是对的,我提供了更多的代码。非常感谢。