IOS - SWIFT - CollectionView With images, loading URLs from array

蹲街弑〆低调 提交于 2020-01-02 23:15:40

问题


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!!


回答1:


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)



回答2:


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

:)



来源:https://stackoverflow.com/questions/30558990/ios-swift-collectionview-with-images-loading-urls-from-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!