如果您正在下載很多圖片,你將有內存問題,和你的工作也將得到扔掉當你的陣列超出範圍,但你可能會想要做什麼,如果你想要實現你提出的解決方案,就是使用字典而不是數組。它會讓您更容易找到您要查找的圖片。所以,你可以實現的字典是這樣的:
var images = [String : UIImage]()
因爲你可以只使用URL字符串(很容易的解決方案)的密鑰,以便訪問圖像安全應該是這樣的:
let urlString = object.imageUrl.absoluteString //or wherever you're getting your url from
if let img = self.images[urlString] {
//Do whatever you want with the image - no need to download as you've already downloaded it.
cell.image = img
} else {
//You need to download the image, because it doesn't exist in your dict
...[DOWNLOAD CODE HERE]...
//Add the image to your dictionary here
self.images[object.imageUrl.absoluteString] = downloadedImage
//And do whatever else you need with it
cell.image = downloadedImage
}
正如我說,這有一些缺點,但它是你要求的一個快速實現。
謝謝,我會試試! –