-2

In my project have added image on my tableview cell, for this I have tried some code but that's not working, what did I do here wrong?

Here my main requirement is I want to round that image (as like circle format).

My code:

 Personimage = [[UIImageView alloc]init];
   Personimage.image = [UIImage imageNamed:@"ram.jpeg"];
   Personimage.translatesAutoresizingMaskIntoConstraints = NO;
   [Cell.contentView addSubview:Personimage];

   //Applying autolayouts

    NSDictionary * viewsDic = NSDictionaryOfVariableBindings(Personimage)

     [Cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[Personimage(80)]"
                                                                                 options:0
                                                                                 metrics:nil
                                                                                   views:viewsDic]];

        [Cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[Personimage(80)]"
                                                                                 options:0
                                                                                 metrics:nil
                                                                                   views:viewsDic]];
}

-(Void)ViewDidAppear
{
  Personimage.layer.cornerRadius = Personimage.frame.size.width / 2;
  Personimage.layer.borderWidth = 3.0f;
  Personimage.layer.borderColor = [UIColor whiteColor].CGColor;
  Personimage.clipsToBounds = YES;
}

After applying auto-layouts i have kept image"round" properties in viewDidAppear method but still it's not working what did i do wrong here? please help me someone

Krish
  • 4,166
  • 11
  • 58
  • 110

2 Answers2

3

You can use this code to round the UIImageView

 imgView.layer.cornerRadius = imgView.frame.size.width / 2;
 imgView.clipsToBounds = YES;
Rahul Patel
  • 5,858
  • 6
  • 46
  • 72
Vidhi Patel
  • 603
  • 5
  • 10
  • no that's not working and i have already tried this in my above code – Krish Sep 16 '15 at 09:37
  • It is working. You should put it in the right place like `viewDidAppear` for example. ;) You should use the half width of your `UIImage`, not `UIImageView`, because it will be `CGSizeZero` before it could be layed out by AutoLayout. You could still use it like this, but as I said, put the code in the right place, after AutoLayout is done its job. – DevAndArtist Sep 16 '15 at 15:41
  • this works fine for me.., thanks Vidhi for sharing the answer – Sai Bharath Yadav Mar 10 '16 at 18:02
0

The property clipsToBounds is a Bool value, change its value from YES to true will work.

Have fun with Swift!

Bright Future
  • 227
  • 3
  • 13