How to add custom image to tableview cell in Swift?

What I need to do is add a circle image to a cell on a uitableview. How can i do this?

I tried some code, but I am not working at all, here is my code:

let imageName = "person.png" let image = UIImage(named: imageName) var imageView = UIImageView(image: image!) imageView = UIImageView(frame: CGRectMake(0, 0, 100, 100)) imageView.layer.borderWidth=1.0 imageView.layer.masksToBounds = false imageView.layer.borderColor = UIColor.whiteColor().CGColor imageView.layer.cornerRadius = 13; imageView.clipsToBounds = true cell.imageView!.image = imageView 

I got this error:

cannot assign a value of type 'UIImageView to a value of type' UIImage? ''

+6
source share
4 answers

You cannot assign an image this way. But U can do it this way.

First add a function that will resize your image:

 func resizeImage(image:UIImage, toTheSize size:CGSize)->UIImage{ var scale = CGFloat(max(size.width/image.size.width, size.height/image.size.height)) var width:CGFloat = image.size.width * scale var height:CGFloat = image.size.height * scale; var rr:CGRect = CGRectMake( 0, 0, width, height); UIGraphicsBeginImageContextWithOptions(size, false, 0); image.drawInRect(rr) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext(); return newImage } 

After that, you can assign an image:

 let imageName = "11749-simple.jpg" let image = UIImage(named: imageName) let newImage = resizeImage(image!, toTheSize: CGSizeMake(70, 70)) var cellImageLayer: CALayer? = cell.cellImage.layer cellImageLayer!.cornerRadius = cellImageLayer!.frame.size.width / 2 cellImageLayer!.masksToBounds = true cell.cellImage.image = newImage 

And the result will be:

enter image description here

Or you can try this extension:

 extension UIImage { var circleMask: UIImage { let square = size.width < size.height ? CGSize(width: size.width, height: size.width) : CGSize(width: size.height, height: size.height) let imageView = UIImageView(frame: CGRect(origin: CGPoint(x: 0, y: 0), size: square)) imageView.contentMode = UIViewContentMode.ScaleAspectFill imageView.image = self imageView.layer.cornerRadius = square.width/2 imageView.layer.borderColor = UIColor.whiteColor().CGColor imageView.layer.borderWidth = 5 imageView.layer.masksToBounds = true UIGraphicsBeginImageContext(imageView.bounds.size) imageView.layer.renderInContext(UIGraphicsGetCurrentContext()) let result = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return result } } 

and you can assign an image:

 let imageName = "11749-simple.jpg" let image = UIImage(named: imageName) cell.cellImage.image = image?.circleMask 

Extension of loans is the answer.

+17
source

Make the one Cell class you want to load into the TableView .

CellComment Class:

 class CellComment: UITableViewCell { @IBOutlet weak var imageV_profile: UIImageView! override func awakeFromNib() { super.awakeFromNib() imageV_profile.layer.cornerRadius = 25 imageV_profile.layer.masksToBounds = true } } 

Now load this cell into the table as shown below.

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell:CellPost! = tableView.dequeueReusableCellWithIdentifier("CellPost") as? CellPost if (cell == nil) { let nib:Array = NSBundle.mainBundle().loadNibNamed("CellPost", owner: self, options: nil) cell = nib[0] as? CellPost } cell.imageV_profile.image = UIImage(named: "Your image name") return cell } 
+1
source
  can you just replace imageView to image let imageName = "person.png" let image = UIImage(named: imageName) var imageView = UIImageView(image: image!) imageView = UIImageView(frame: CGRectMake(0, 0, 100, 100)) imageView.layer.borderWidth=1.0 imageView.layer.masksToBounds = false imageView.layer.borderColor = UIColor.whiteColor().CGColor imageView.layer.cornerRadius = 13; imageView.clipsToBounds = true cell.imageView!.image = image 
+1
source

Try this code. This gives exactly what you want ....

 let imageName = "person.png" let image1 = UIImage(named: imageName) var imageView = UIImageView(frame: CGRectMake(5, 5, 100, 100)) imageView.layer.borderWidth=1.0 imageView.layer.masksToBounds = true imageView.layer.borderColor = UIColor.whiteColor().CGColor imageView.layer.cornerRadius = 50;// Corner radius should be half of the height and width. imageView.image = image1 cell.addSubview(imageView) 
0
source

Source: https://habr.com/ru/post/986542/


All Articles