omkar marathe
omkar marathe

Reputation: 61

Images are not setting properly to the collectionview cell

I am using a collection view and it contains an imageview. Images are in url format so I have written a function to convert the url into Image but images are not looking properly. I have tried all the modes like aspect fit, aspect fill, scale to fill. I have searched a lot on the internet but not come up with answer.

This is my code :

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)
                    }
                }
            }
        }
    }

Upvotes: 0

Views: 84

Answers (1)

ChooGoo
ChooGoo

Reputation: 105

Describe your problem please, attach screenshot what’s wrong. Seems like the problem is in the image, try setting the different content mode and if needed — clipsToBounds = true

Upvotes: 0

Related Questions