2017-03-01 49 views
0

我的应用程序中有一个工作的UIScroll视图和本地图像。然而,我想要的是,我的图片将从网址下载并存储在缓存中。我见过几个类似sdwebimage,翠鸟等的示例库,但这些示例使用UITableview和单元格。我为我的滚动视图使用UIImage数组。我真正想要的是我下载并缓存我的图像并将它们存储在Array IconsArray = [icon1,icon2,icon3],其中icon1到icon3是从URL下载的图像。我将如何做到这一点?任何漂亮的教程或者有足够的人来向新秀展示一些代码?如何将URL中的图像加载到UIImage数组中并在UIScrollView中将它们用于Swift中

在此先感谢

回答

0

如果您正在下载很多图片,你将有内存问题,和你的工作也将得到扔掉当你的阵列超出范围,但你可能会想要做什么,如果你想要实现你提出的解决方案,就是使用字典而不是数组。它会让您更容易找到您要查找的图片。所以,你可以实现的字典是这样的:

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 
} 

正如我说,这有一些缺点,但它是你要求的一个快速实现。

+0

谢谢,我会试试! –

相关问题