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

后端 未结 2 1864
伪装坚强ぢ
伪装坚强ぢ 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())
    }
    

提交回复
热议问题