程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Swift Alamofire Image 下载并分配给 collectionView 中的 ImageView大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Swift Alamofire Image 下载并分配给 collectionView 中的 ImageView?

开发过程中遇到Swift Alamofire Image 下载并分配给 collectionView 中的 ImageView的问题如何解决?下面主要结合日常开发的经验,给出你关于Swift Alamofire Image 下载并分配给 collectionView 中的 ImageView的解决方法建议,希望对你解决Swift Alamofire Image 下载并分配给 collectionView 中的 ImageView有所启发或帮助;

我正在使用 alamofire 从 API 返回下载图像,API 返回保存图像 url,我使用 alamofire 在 for 循环中从这些 url 下载图像数据,但由于异步方法,数组中的图像未按顺序排序.此外,即使我通过在主线程中使用 reloadData() 将图像分配给 collectionvIEw,在视图初始化后我也看不到图像,但是当我滚动时会显示 collectionVIEw 图像。我将图像存储在一个数组中。

func fetchData(){
    AF.request("https://API.rawg.io/API/games?key=ab904f882bf94fa9960857911498aa78",method: .get,parameters: nil,headers: nil,interceptor: nil).valIDate(statusCode: 200 ..< 299).responseJsON { AFdata in
                

                    do{
                        let game = try
                            JsONDecoder().decode(Game.self,from: AFdata.data!)
                    
                        self.games = game
                        
                        if let gameResults = self.games?.results{
                            
                            for gameItem in gameResults{
                                AF.request(gameItem.BACkgroundImage!,interceptor: nil).valIDate(statusCode: 200 ..< 299).responseData
                                    { AFdata in
                                        dispatchQueue.main.async {
                                             self.gameImages.append(UIImage(data: AFdata.data!)!)
                                        }
                                       
                                }
                                
                            }
                        }
                        
                        dispatchQueue.main.async {
                            self.gamesCollectionVIEw.reloadData()
                        }
                    
                    }catch let JsonErr{
                        print(JsonErr)
                    }
            }
}

func collectionVIEw(_ collectionVIEw: UICollectionVIEw,cellForItemAt indexPath: IndexPath) -> UICollectionVIEwCell {
    
    let cell = collectionVIEw.dequeueReusableCell(withReusEIDentifIEr: GameCollectionVIEwCell.IDentifIEr,for: indexPath) as! GameCollectionVIEwCell
    
    cell.BACkgroundcolor = .blue
    cell.gamenameLabel.text = games?.results?[indexPath.item].name
    
    if(gameImages.count != games?.results?.count){
        cell.imageVIEw.image = nil
    }else
    {
        cell.imageVIEw.image = gameImages[indexPath.item]
    }
    
    return cell

}

}

解决方法

使用DispatchGroup

这里是演示

func fetchData(){
    AF.request("https://api.rawg.io/api/games?key=ab904f882bf94fa9960857911498aa78",method: .get,parameters: nil,headers: nil,interceptor: nil).validate(statusCode: 200 ..< 299).responseJSON { AFdata in
        
        
        do{
            let game = try
                JSONDecoder().decode(Game.self,from: AFdata.data!)
            
            self.games = game
            
            if let gameResults = self.games?.results{
                let dispatchGroup = DispatchGroup() //<--- Here
                
                for gameItem in gameResults{
                    AF.request(gameItem.BACkgroundImage!,interceptor: nil).validate(statusCode: 200 ..< 299).responseData
                    {
                        dispatchGroup.leave() //<--- Here
                        AFdata in
                        DispatchQueue.main.async {
                            self.gameImages.append(UIImage(data: AFdata.data!)!)
                        }
                        
                    }
                    
                }
            }
            dispatchGroup.notify(queue: .main) {
                self.gamesCollectionView.reloadData() //<--- Here
            }
            
            
        }catch let jsonErr{
            print(jsonErr)
        }
    }
}

,

您需要在追加游戏图像之前在 AF.request 组合块内调用 reload collectionView 函数。因为它需要时间来获取数据,但是在获取图像之前您正在重新加载 collectionview。

func fetchData(){
AF.request("https://api.rawg.io/api/games?key=ab904f882bf94fa9960857911498aa78",interceptor: nil).validate(statusCode: 200 ..< 299).responseJSON { AFdata in
            

                do{
                    let game = try
                        JSONDecoder().decode(Game.self,from: AFdata.data!)
                
                    self.games = game
                    
                    if let gameResults = self.games?.results{
                        
                        for gameItem in gameResults{
                            AF.request(gameItem.BACkgroundImage!,interceptor: nil).validate(statusCode: 200 ..< 299).responseData
                                { AFdata in
                                    DispatchQueue.main.async {
                                         self.gameImages.append(UIImage(data: AFdata.data!)!)
                        self.gamesCollectionView.reloadData()

                                    }
                                   
                            }
                            
                        }
                    }
                
                }catch let jsonErr{
                    print(jsonErr)
                }
        }

}

大佬总结

以上是大佬教程为你收集整理的Swift Alamofire Image 下载并分配给 collectionView 中的 ImageView全部内容,希望文章能够帮你解决Swift Alamofire Image 下载并分配给 collectionView 中的 ImageView所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。