IOS - SWIFT - CollectionView使用图像,从数组加载URL [英] IOS - SWIFT - CollectionView With images, loading URLs from array

查看:191
本文介绍了IOS - SWIFT - CollectionView使用图像,从数组加载URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个集合视图和数组,其中包含不同图像的URL。当我启动应用程序时,集合视图开始通过数组加载图像:

I have a collection view and array with URLs of different images. and when i launch the app, collection view starts to load images through the array:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
    var cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CellView

    var url = arr[indexPath.row]

    var urls = NSURL(string: url)
    var data = NSData(contentsOfURL: urls!)

    cell.ImageView.image = UIImage(data: data!)

    return cell
}

并且出现问题:
例如在第4个单元格集合视图中加载所有4个单元格的所有4个网址并且需要很多时间。集合视图如何加载特定单元格的特定URL,并且不花时间将URL加载到已加载的单元格?

and the trouble appearse: for example on 4th cell collection view loading all 4 urls for all 4 cells and it takest alot time. how can collection view load particular url for particular cell and don't spend time to load urls to cells that already loaded?

感谢您的帮助!!

推荐答案

我建议使用这个问题的第三方库,它叫做 SDWebImage

I suggest using a third party library for this matter, it called SDWebImage.

而不是单元格集内的每个图像视图:

And than for each image view inside a cell set:

self.imageView.sd_setImageWithURL(url, completed: block)

这篇关于IOS - SWIFT - CollectionView使用图像,从数组加载URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆