将图像从URL加载到表视图单元格中的图像视图的更快方法
问题描述:
我用它来加载URL到图像视图
I am using this to load URL to image view
let url = NSURL(string: URL)
let data = NSData(contentsOfURL: url!)
self.releasephoto.image = UIImage(data: data!)
但是当表视图单元格中有10个项目有不同的图像时,滚动变慢并挂起
But when there is 10 items in table view cell with different images, scrolling gets slower and hangs
我加载大小为40 X 40的图像
I load images with the size of 40 X 40
我想知道是否有更好的方法来加载URL图像?
I wonder if there is a better way to load URL images ?
答
我建议使用 SDWebImage 。
安装完成后,你需要为Swift创建一个桥接头并包括这一行:
After installing it you'll need to make a bridging header for Swift and include this line:
#import "UIImageView+WebCache.h"
然后你需要在你的cellForRowAtIndexPath函数中做的就是:
Then all you need to do in your cellForRowAtIndexPath function is this:
let url = NSURL(string: URL)
self.releasephoto.sd_setImageWithURL(url)
这个库的优点在于它会自动缓存图像,因此只下载一次。
The nice thing about this library is that it automatically caches the image so it's only downloaded once.