IOS - SWIFT - CollectionView With images, loading

2019-06-04 03:01发布

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
}

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?

Thanks for any help!!

2条回答
forever°为你锁心
2楼-- · 2019-06-04 03:28

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)
查看更多
神经病院院长
3楼-- · 2019-06-04 03:48

Or you can use similar third party library, as Asaf, told you. I used to use HANEKE for DL/cache images.

Take a look: https://github.com/Haneke/HanekeSwift

:)

查看更多
登录 后发表回答