Image not round

cell.imageView!.image = UIImage(data: data)
                                cell.imageView?.layer.borderWidth=1.0
                                cell.imageView?.layer.masksToBounds = true
                                cell.imageView?.layer.borderColor = UIColor.white.cgColor
                                cell.imageView?.layer.cornerRadius = (cell.imageView?.frame.size.height)!/2
                                cell.imageView?.clipsToBounds = true

I added this code to make the image in the cell round. But it is not round. Can anyone tell me why? And help me? Thanks

Replies

I've tried your code and tested the value of cell.imageView?.frame.size.height


At this point in code, it is not yet set, hence a value of 1.


If you replace by a fix value that you compute, like


cell.imageView?.layer.cornerRadius = 30 // (cell.imageView?.frame.size.height)!/2


You should see some effect


Here is the complete part of code, in tableView(cellForRowAt)


                   let itemSize = CGSize.init(width: 80, height: 80) 
                   let imageRect : CGRect = CGRect(x: 0, y: 0, width: 80, height: 80)
                   UIGraphicsBeginImageContextWithOptions(itemSize, true, UIScreen.main.scale);
                    cell.imageView?.layer.borderWidth=1.0
                    cell.imageView?.layer.masksToBounds = true
                    cell.imageView?.layer.borderColor = UIColor.white.cgColor
                    cell.imageView?.layer.cornerRadius = 40
                    cell.imageView?.clipsToBounds = true
                    cell.imageView?.image!.draw(in: imageRect)
                    cell.imageView?.image! = UIGraphicsGetImageFromCurrentImageContext()!;
                    UIGraphicsEndImageContext()

Thank you but it crashes on this line:

        cell.imageView?.image!.draw(in: imageRect)

oh yes. I copied from other code.


you have to set image view?.image before With


cell.imageView!.image = UIImage(data: data)