iosswiftobjective-cuiimage

How to crop UIImage on oval shape or circle shape?


Please give ideas for how to crop UIImage on oval shape or circle shape. Please share your ideas.


Solution

  • #import <QuartzCore/QuartzCore.h>
    
    CALayer *imageLayer = YourImageview.layer;
            [imageLayer setCornerRadius:5];
            [imageLayer setBorderWidth:1];
            [imageLayer setMasksToBounds:YES];
    

    by increasing radius it will become more round-able.

    As long as the image is a square, you can get a perfect circle by taking half the width as the corner radius:

    [imageView.layer setCornerRadius:imageView.frame.size.width/2]; 
    

    You also need to add

    [imageView.layer setMasksToBounds:YES];
    

    Swift 4.2

    import QuartzCore
    
    var imageLayer: CALayer? = YourImageview.layer
    imageLayer?.cornerRadius = 5
    imageLayer?.borderWidth = 1
    imageLayer?.masksToBounds = true