如果您正在下载很多图片,你将有内存问题,和你的工作也将得到扔掉当你的阵列超出范围,但你可能会想要做什么,如果你想要实现你提出的解决方案,就是使用字典而不是数组。它会让您更容易找到您要查找的图片。所以,你可以实现的字典是这样的:
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
}
正如我说,这有一些缺点,但它是你要求的一个快速实现。
谢谢,我会试试! –