Illep
Illep

Reputation: 16841

set imageview corner radius

I tried most of the workaround to set the corner radius of aimageview found in a cell.

However, it still displays a box. How can i resolve this ?

cell.imageView.image=[UIImage imageNamed:@"def.png"];
cell.imageView.layer.cornerRadius=cell.imageView.frame.size.width/2;
cell.imageView.layer.borderWidth=3.0f;
cell.imageView.layer.borderColor=[UIColor redColor].CGColor;
cell.imageView.layer.masksToBounds = YES;
cell.imageView.layer.shouldRasterize = YES;

Upvotes: 0

Views: 393

Answers (4)

Alaeddine
Alaeddine

Reputation: 6212

Try to replace masksToBounds by clipsToBounds

cell.imageView.layer.cornerRadius = cell.imageView.frame.size.width/2
cell.imageView.clipsToBounds = YES

make sure that you have the correct width in cell.imageView.frame.size.width for example if you use autolayout for the width of the UIImageView you need to move your code to viewDidLayoutSubviews after width calculation

A view can optionally limit the drawing of its subviews so that any parts of them outside the view are not shown. This is called clipping and is set with the view’s clipsToBounds property.

Upvotes: 1

Fabio Berger
Fabio Berger

Reputation: 1917

Im think the cell overrides the imageView. Try pasting that code in the layoutSubviews method or add a second imageView to the cell.

In code it would look like this for the layoutSubviews option:

-(void)layoutSubviews
{
    [super layoutSubviews];

    cell.imageView.image=[UIImage imageNamed:@"def.png"];

    cell.imageView.layer.cornerRadius=cell.imageView.frame.size.width/2;

    cell.imageView.layer.borderWidth=3.0f;

    cell.imageView.layer.borderColor=[UIColor redColor].CGColor;

    cell.imageView.layer.masksToBounds = YES;

    cell.imageView.layer.shouldRasterize = YES;
}

And for a second imageView like this:

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"def.png"]];
imageView.frame = CGRectMake(0, 0, 44, 44);
imageView.layer.cornerRadius = imageView.frame.size.width/2;
imageView.layer.borderWidth = 3.0f;   
imageView.layer.borderColor = [UIColor redColor].CGColor;   
imageView.layer.masksToBounds = YES;   
imageView.layer.shouldRasterize = YES;   
[self addSubview:imageView];

Upvotes: 0

Fahim Parkar
Fahim Parkar

Reputation: 31627

You are just missing clipsToBounds statement.

cell.imageView.clipsToBounds = YES;

Add this and let me know what is the result...

If this don't work, try with cell.clipsToBounds = YES;

For sure, it will work...

Upvotes: 0

Bad_Developer
Bad_Developer

Reputation: 537

Try to import QuartzCore then using layer property of UIImageView like this:

yourImageView.layer.cornerRadius = 10.0f;
yourImageView.clipsToBounds = YES;

Upvotes: 1

Related Questions