0

Facing problem when adding rounded corners to imageview. here is my code

CALayer * lay = [imageview layer];
[lay setMasksToBounds:YES];
[lay setCornerRadius:10.0];

[lay setBorderWidth:5.0];
[lay setBorderColor:[[UIColor clearColor] CGColor]];

imageview.layer.rasterizationScale = [UIScreen mainScreen].scale;
imageview.layer.shouldRasterize = YES;

And its working fine but if I am resizing the imageview, its not making corners round.

Here is my resizing code:

[UIView animateWithDuration:0.2 animations:^ {
    [imageview setTransform:CGAffineTransformMakeScale(1.1, 1.1)];

    CALayer * l = [imageview layer];
    [l setMasksToBounds:YES];
    [l setCornerRadius:10.0];

    [l setBorderWidth:5.0];
    [l setBorderColor:[[UIColor clearColor] CGColor]];

    imageview.layer.rasterizationScale = [UIScreen mainScreen].scale;
    imageview.layer.shouldRasterize = YES;

    newimage = imageview.image;

    [[NSUserDefaults standardUserDefaults] setValue:UIImageJPEGRepresentation(newimage,1.1) forKey:@"newimage"];   

}];

Any help would be appreciated.

Fabio Berger
  • 1,921
  • 2
  • 24
  • 29
Priyanta Singh
  • 77
  • 1
  • 12

3 Answers3

3

You can set corner radius from storyboard or XIB hope it will work, Please check This.

Community
  • 1
  • 1
Ashish Thummar
  • 431
  • 4
  • 10
0

Plz import the QuartzCore/QuartzCore.h

and then write the code ...

yourImageView.layer.cornerRadius = yourRadius;

yourImageView.clipsToBounds = YES;

Why to write the clipsToBounds property

A Boolean value that determines whether subviews are confined to the bounds of the view.

Discussion Setting this value to YES causes subviews to be clipped to the bounds of the receiver. If set to NO, subviews whose frames extend beyond the visible bounds of the receiver are not clipped. The default value is NO.

Vipul
  • 111
  • 1
  • 4
-1

Try

[UIView animateWithDuration:0.2
                          delay:0.1
                        options: UIViewAnimationOptionCurveEaseOut
                     animations:^
     {
        [imageview setTransform:CGAffineTransformMakeScale(1.1, 1.1)];
     }
     completion:^(BOOL finished)
     {
         [imageview.layer setCornerRadius:10.0];
         imageview.clipsToBounds = YES;
         imageview.layer.masksToBounds = YES;
         [imageview.layer setBorderWidth:5.0];
         [imageview.layer setBorderColor:[[UIColor clearColor] CGColor]];
         newimage = imageview.image;

         [[NSUserDefaults standardUserDefaults] setValue:UIImageJPEGRepresentation(newimage,1.1) forKey:@"newimage"];
     }];

You have to round corners after animation complete. You may get animation completion as above.

Rahul Patel
  • 5,858
  • 6
  • 46
  • 72