How to mask a square image into an image with round corners in iOS?

前端 未结 8 1708
[愿得一人]
[愿得一人] 2020-11-28 01:20

How can you mask a square image into an image with round corners?

8条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-28 02:09

    Here is an even easier method that is available in iPhone 3.0 and up. Every View-based object has an associated layer. Each layer can have a corner radius set, this will give you just what you want:

    UIImageView * roundedView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"wood.jpg"]];
    // Get the Layer of any view
    CALayer * layer = [roundedView layer];
    [layer setMasksToBounds:YES];
    [layer setCornerRadius:10.0];
    
    // You can even add a border
    [layer setBorderWidth:4.0];
    [layer setBorderColor:[[UIColor blueColor] CGColor]];
    

    To use these methods you might need to add:

    #import 
    

提交回复
热议问题