0
我按照教程加載圖像URL並將它們附加到數組,以便使用以下代碼塊在每個用戶的單元格的UITableView中顯示用戶配置文件圖像:Firebase Swift 3使用NSURLSession將配置文件圖像加載到TableView中
let url = NSURL(string: urlString)
NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data, response, error) in
//download hit an error so lets return out
if error != nil {
print(error)
return
}
dispatch_async(dispatch_get_main_queue(), {
//"profilePics" is my array of UIImages made as a global var
self.profileImages.append(UIImage[data!])
})
}).resume()
但出於某種原因「NSURLSession」,而我編程並返回錯誤,所以我從改變了一切不顯示爲一個建議「NSURL ......」簡單的「網址...」:
//Grabbing images from db
print("starting get image block : ")
let url = URL(string: profileImageURL)
URLSession.shared.dataTask(with: url!, completionHandler: {(data, response, error) in
print("grabbing image: ") //Test line for monitor
print(String(describing: data)) //Test line for monitor : returns "nil"
if error != nil{
print(error!)
return
}
DispatchQueue.main.async {
//"profilePics" is my array of UIImages made as a global var
self.profilePics.append(UIImage(data: data!))
print("Image appended")
}
}).resume()
// grabbing images from db end
但每次我運行這個我得到一個零數據,一個它永遠不會附加到我的數組。有人可以解釋我缺少的東西嗎?這是我第一次嘗試使用Firebase。
你爲什麼要添加數據的NSArray它是不好的做法 –
@kirtimali你會建議? – 1QuickQuestion
檢查此問題http://stackoverflow.com/questions/24231680/loading-downloading-image-from-url-on-swift –