~ 1300 이미지를 다운로드 중입니다. 그것들은 총 이미지 크기가 약 500KB 정도되는 작은 이미지입니다. 그러나 다운로드 userDefault에 넣어 후에, 나는 다음과 같은 오류가 발생합니다 :Swift를 사용하여 열린 파일을 어떻게 닫습니까?
libsystem_network.dylib을 : nw_route_get_ifindex :: 소켓 (PF_ROUTE, SOCK_RAW, PF_ROUTE가) 실패 : [24] 열려있는 파일이 너무 많습니다
아마 다운로드 한 png 이미지가 닫히지 않고있는 것입니다.
나는 이미 확장 캐시 크기를 아래로 :
func storeImages(){
for i in stride(from: 0, to: Cur.count, by: 1) {
// Saving into userDefault
saveIconsToDefault(row: i)
}
}
내가 그들 모두 후 오류가 userDefault에 추가되는 :
// 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
그리고 이것은 내가 이미지를 저장할 수있어 접근 방식입니다. 그래서, 나는 그들이 거기 있다는 것을 알고있다.
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 try this this –