-1

I would like to add some rounded corners to the UIImageView in my project. I wrote following code to implement this but this is not working for me.

Any suggestion ?

btnProfilePic.layer.cornerRadius = 50;
Tejas Patel
  • 850
  • 10
  • 26
  • possible duplicate of [Add rounded corners to all UIImageViews](http://stackoverflow.com/questions/2171506/add-rounded-corners-to-all-uiimageviews) – NANNAV Aug 10 '15 at 07:06
  • No this is not a duplicate question. I search on internet bt still getting littlebit confused that's why i asked this question. – Tejas Patel Aug 13 '15 at 17:23

4 Answers4

3

You need to set masksToBounds to true

self.imageview.layer.cornerRadius = 50;
self.imageview.layer.masksToBounds = true;
Leo
  • 24,596
  • 11
  • 71
  • 92
0

setMasksToBound to yes if your UIImageView..

[imgView1.layer setCornerRadius:50];
[imgView1.layer setMasksToBounds:YES];
krushnsinh
  • 874
  • 1
  • 10
  • 11
0
btnProfilePic.layer.cornerRadius = btnProfilePic.frame.size.height/2;
btnProfilePic.layer.masksToBounds = YES;

And for improving performance you can choose to set

btnProfilePic.layer.shouldRasterize = YES;

Note: For view you are going to round its height and width must be same.

Akshay Sunderwani
  • 12,428
  • 8
  • 29
  • 52
-1

All solutions with layer is very good, and work very clear. The solutions with layer are very expensive for the performance of animation, but very cheap for implementation.

You can use other solution (I use it because have very many small imageViews in a list with scrolling). You can prepare image with rounded corners (and borders if need it) for display it in the imageView. Set backgroundColor for the imageView as clear or same with a parent view and set prepared image to the imageView.

To resize images I use this library to add rounded corners I've wrote custom method (it builds rounded corners path to use it for clip image), for cache resized images I use SDWebImageCache. All operations I run at a background queue. It solution more complex, but you can use it if you have many images and you need best performance for scrolling animation.

Andrew Romanov
  • 4,774
  • 3
  • 25
  • 40