Swift: download image from Internet and cache them doesn't work properly. need suggestions

后端 未结 2 1839
伪装坚强ぢ
伪装坚强ぢ 2021-01-28 18:09

I am new to swift, and I am building an App that download images from the Internet and display in a UICollectionView, I can achieve this function successfully, however, it seems

相关标签:
2条回答
  • 2021-01-28 18:31

    Use AlamofireImage

    Swift 2.2 or 2.3

    Add --> pod 'AlamofireImage', '~> 2.5' in your pod file.

    Swift 3.1

    Add --> pod 'AlamofireImage', '~> 3.3' in your pod file.

    Put the following line in your code which will automatically cache the image and download it as well.

    override func viewDidLoad() {
         super.viewDidLoad()
         //hit your web service here to get all url's
     }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
       return arrImageURL.count
    }
    
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
    { 
       cell.imgViewPhoto.af_setImageWithURL(arrImageURL[indexPath.row], placeholderImage: UIImage())
    }
    
    0 讨论(0)
  • 2021-01-28 18:49

    Kingfisher is a lightweight and pure Swift implemented library for downloading and caching image from the web. This project is heavily inspired by the popular SDWebImage. And it provides you a chance to use pure Swift alternative in your next app. kingfisher

    0 讨论(0)
提交回复
热议问题