〜1300枚の画像をダウンロードしています。それらは合計サイズが約500KBである小さな画像です。ただし、ダウンロードしuserDefaultにそれらを入れた後、私は以下のようにエラーが表示されます。Swiftを使用して開いているファイルをどのように閉じますか?
libsystem_network.dylib:nw_route_get_ifindex ::ソケット(PF_ROUTE、SOCK_RAW、PF_ROUTE)が失敗しました:[24]開いているファイルが多すぎます
おそらく、ダウンロードされたPNG画像は閉じられていません。下記経由
I既に拡張キャッシュサイズ:
// 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に追加された後にエラーが発生します。だから、私は彼らがそこにいることを知っている。
EDIT:
機能:
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)
}
}
}
}
USAGE:
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
}
'UIImage'sを' UserDefaults'に保存しないでください。決してこれまでにない。特に1300画像ではありません。 – the4kman
入力いただきありがとうございます。しかし、それらを置く場所を指摘すれば、それほど良くないでしょうか? – sc13
https://stackoverflow.com/questions/6238139/ios-download-and-save-image-inside-appこれを試してください –