图片未正确设置到collectionview单元格

问题描述

我正在使用集合视图,它包含一个imageview。图片采用url格式,因此我编写了一个将url转换为Image的功能,但是图片显示不正确。 我尝试了所有模式,例如长宽比适合,长宽比填充,缩放以填充。 我在互联网上进行了很多搜索,但没有找到答案。

这是我的代码:

extension HomeDemoViewController : UICollectionViewDelegate,UICollectionViewDataSource{
    
    func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
        return bannerArray.count
    }
    func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HomeAddCollectionViewCell",for: indexPath)
            as! HomeAddCollectionViewCell

        cell.imageview.setImageFromURl(stringImageUrl: imageArray[indexPath.row])
        
        return cell
    }
    func collectionView(_ collectionView: UICollectionView,didSelectItemAt indexPath: IndexPath) {
        let vc = self.storyboard!.instantiateViewController(withIdentifier: "MenuViewController") as! MenuViewController
        self.navigationController!.pushViewController(vc,animated: true)
    }
}



 func setImageFromURl(stringImageUrl url: String){
        if let url = NSURL(string: url) {
            DispatchQueue.global(qos: .default).async{
                if let data = NSData(contentsOf: url as URL) {
                    DispatchQueue.main.async {
                        self.image = UIImage(data: data as Data)
                    }
                }
            }
        }
    }

解决方法

请描述您的问题,并附上屏幕截图。 似乎问题出在图像上,请尝试设置其他内容模式,如果需要,请尝试-clipsToBounds = true

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...