简体   繁体   中英

set imageview corner radius

I tried most of the workaround to set the corner radius of a imageview 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;

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.

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

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

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];

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM