Setting Corner Radius on UIImageView not working

IphoneIosUiimageviewRounded Corners

Iphone Problem Overview


I'm at a bit of a loss. I've used the layer property of UIView to round the corners of multiple elements in my app. However, this one UIImageView is simply not complying. Not sure what I am missing.

The UIImageView (called previewImage) is contained in a Table View Cell. I've tried setting the cornerRadius property multiple location (in the cell itself and in the controller that creates the cell) to no avail.

static NSString *CellIdentifier = @"MyTableViewCell";

MyTableViewCell *cell = (MyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
	cell = [topLevelObjects objectAtIndex:0];
    cell.previewImage.layer.cornerRadius = 20; //Made it 20 to make sure it's obvious.
}

Is there something about the way cells are loaded that I'm missing?

Iphone Solutions


Solution 1 - Iphone

You need to set the layer's masksToBounds property to YES:

cell.previewImage.layer.masksToBounds = YES;

This is because the UIImageView control creates a pseudo-subview to hold the UIImage object.

Solution 2 - Iphone

Also worth noting that

  1. If you are using aspectFit AND cornerRadius with clipsToBounds/masksToBounds, you won't get the rounded corners.

i.e if you have this

theImageView.contentMode = .scaleAspectFit

and

   theImageView.layer.cornerRadius = (theImageView.frame.size.height)/2
    theImageView.clipsToBounds = true

or

theImageView.layer.masksToBounds = true

It won't work. you will have to get rid of aspectFit code

//theImageView.contentMode = .scaleAspectFit

2. Make sure the width and the height for the Image View is same

Solution 3 - Iphone

This should work

cell.previewImage.clipsToBounds = YES;

cell.previewImage.layer.cornerRadius = 20;

Solution 4 - Iphone

I believe you need to set:

cell.previewImage.layer.masksToBounds = YES;
cell.previewImage.layer.opaque = NO;

Solution 5 - Iphone

In Xcode Interface Builder, selecting 'Clip Subviews' Drawing attribute for the view together with setting the corner radius in the code cell.previewImage.layer.cornerRadius = 20;does the job for me!

See 'Clip Subviews' option in IB

Solution 6 - Iphone

Try this below piece of code

cell.previewImage.layer.cornerRadius = 20;
cell.previewImage.clipsToBounds = YES;

Solution 7 - Iphone

Try this code:-

self.imgaviewName.clipsToBounds = true
self.imageviewName.layer.cornerRadius = 10

Solution 8 - Iphone

For imageView.contentMode = .scaleAspectFill the cornerRadius is not applied if the image is very large, depending on the hardware.

Some tests with resized panorama images:

  • iPhone X, image size 5000x1107:  4000x886: ✅
  • iPhone 6s Plus, image size 10000x2215:  5000x1107: ✅
  • iPhone 6s, image size 10000x2215:  5000x1107: ✅

The imageView dimensions where 160x100. Interestingly, the newer hardware isn't necessarily more capable.

Feel free to edit this post with more test results!

Solution 9 - Iphone

Swift 4.2 Answer:

In order for the corner radius to work, add the image to a UIView and then you need to set the image's masksToBounds property to true:

planeImage.layer.masksToBounds = true
planeImage.layer.cornerRadius = 20

Note: Replace 20 by the desired cornerRadius

Solution 10 - Iphone

Nothing from the previous answers is working?

It may happen that the size of the UIImageView is bigger than the image size. Corner radius can be set well but not visible in that case.

Quick check of the UIImageView size by code: (or can use "View UI Hierarchy" tool in XCode)

self.imageView.backgroundColor = [UIColor greenColor]; 

In this scenario you should assure that UIImageView has the same aspect ratio as the image.

Solution 11 - Iphone

UIImageView gives cornerRadius with clipsToBounds folowing is example of Swift and Objectiv-C (The followfing example code for round image)

Swift

myImageView.layer.cornerRadius = myImageView.frame.size.height/2.0
myImageView.clipsToBounds = true

Objectiv -C

[myImageView.layer setCornerRadius:myImageView.frame.size.height/2.0];
[myImageView setClipsToBounds:TRUE];

Solution 12 - Iphone

-(void) viewDidAppear:(BOOL)animated{
       [super viewDidAppear:animated];
       [self setMaskTo:viewDistance 
             byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight];
           }

- (void)setMaskTo:(UIView*)view byRoundingCorners:(UIRectCorner)corners
     {
      UIBezierPath *rounded = [UIBezierPath 
                bezierPathWithRoundedRect:view.bounds
                                              byRoundingCorners:corners
                                                    
                     cornerRadii:CGSizeMake(20.0, 20.0)];

          CAShapeLayer *shape = [[CAShapeLayer alloc] init];
          shape.frame = self.view.bounds;
         [shape setPath:rounded.CGPath];
          view.layer.mask = shape;
       }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMarkPowellView Question on Stackoverflow
Solution 1 - IphoneEvan MulawskiView Answer on Stackoverflow
Solution 2 - IphoneNaishtaView Answer on Stackoverflow
Solution 3 - IphoneTeena nath PaulView Answer on Stackoverflow
Solution 4 - IphoneDaniel DickisonView Answer on Stackoverflow
Solution 5 - IphonekoiraView Answer on Stackoverflow
Solution 6 - IphoneVaibhav ThakreView Answer on Stackoverflow
Solution 7 - Iphonekangna sharmaView Answer on Stackoverflow
Solution 8 - IphoneOrtwin GentzView Answer on Stackoverflow
Solution 9 - IphoneIronmanX46View Answer on Stackoverflow
Solution 10 - IphoneMarek ManduchView Answer on Stackoverflow
Solution 11 - IphoneBera BhavinView Answer on Stackoverflow
Solution 12 - IphoneMaulik PatelView Answer on Stackoverflow