2017-11-21 4 views
3

〜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 
} 
+2

'UIImage'sを' UserDefaults'に保存しないでください。決してこれまでにない。特に1300画像ではありません。 – the4kman

+0

入力いただきありがとうございます。しかし、それらを置く場所を指摘すれば、それほど良くないでしょうか? – sc13

+0

https://stackoverflow.com/questions/6238139/ios-download-and-save-image-inside-appこれを試してください –

答えて

1

URLSession(configuration: .default)構文は、要求ごとに新しいURLSessionを作成しています。 1つのURLSessionを作成し(一部のプロパティに保存)、すべての要求に対して再利用します。それとも、あなたが本当にURLSessionのいずれかのカスタム設定を行っていない場合は、ちょうどURLSession.shared使用:

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

をあなたはUserDefaultsで1300枚の画像を保存していることに言及します。これは、その種類のデータを格納する場所でも、その量のファイルでも適切な場所ではありません。私はあなたがFile System Programming Guide: The Library Directory Stores App-Specific Filesに概説されているように "キャッシュ"フォルダを使用することをお勧めします。

let cacheURL = try! FileManager.default 
    .url(for: .cachesDirectory, in: .userDomainMask, appropriateFor: nil, create: true) 
    .appendingPathComponent("images") 

// create your subdirectory before you try to save files into it 
try? FileManager.default.createDirectory(at: cacheURL, withIntermediateDirectories: true) 

"Documents"フォルダにも保存しないでください。詳細については、iOS Storage Best Practicesを参照してください。

関連する問題