0

How to change the size of my UIImageView in a table view to be a circle? The snippet below is taken from the table view data source.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
    let myCell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as UITableViewCell
        switch(mySegmentedControl.selectedSegmentIndex)
        {
        case 0 :
                myCell.textLabel?.text = aaa[indexPath.row] as? String
                myCell.imageView?.image = images[indexPath.row]
            break
        case 1 :
            myCell.textLabel?.text = bbb[indexPath.row] as? String
                myCell.imageView?.image = nil
            break
        case 2 :
            myCell.textLabel?.text =  cccc[indexPath.row] as? String
                myCell.imageView?.image = nil
            break
        default:
            break
        }
    return myCell
    }
  • Possible duplicate of [How to make a circular UIView](http://stackoverflow.com/questions/1878595/how-to-make-a-circular-uiview) – wczekalski Nov 14 '15 at 14:43
  • Possible duplicate of [Circular UIImageView in UITableView without performance hit?](http://stackoverflow.com/questions/17721934/circular-uiimageview-in-uitableview-without-performance-hit) – Kevin Nov 15 '15 at 04:25

1 Answers1

0

What you are probably looking at is changing the shape of the UIImageView not the UIImage itself. The easiest way to do that is to add a mask to the layer:

let layer = myCell.imageView?.layer
layer.cornerRadius = layer.frame.size.height/2
layer.masksToBounds = true
wczekalski
  • 720
  • 4
  • 21
  • I still have the same problem, some image are bigger than others, –  Nov 14 '15 at 22:02
  • do your images have the same sizes ? Do you modify the frame or bounds of the image views anywhere? – wczekalski Nov 14 '15 at 22:42
  • No the images don't have the same sizes. and no I'm no modifying the bounds of the image cell anywhere else –  Nov 14 '15 at 23:05
  • So what you are trying to do is to stretch the images. Well, it won't look good, but here you go with how to do it: http://stackoverflow.com/questions/4895272/difference-between-uiviewcontentmodescaleaspectfit-and-uiviewcontentmodescaletof – wczekalski Nov 15 '15 at 08:32