Making a UIImage to a circle form

后端 未结 10 2066
南旧
南旧 2020-12-02 17:16

I have been trying to mask a UIImage into a circle. I\'m using now the code that has been popular on other answers here, but although I do get a circle its edges are very ja

10条回答
  •  旧巷少年郎
    2020-12-02 17:41

    What about getting help from UIImageView

    + (UIImage *)circularImage:(UIImage *)image withDiameter:(NSUInteger)diameter
    {
        CGRect frame = CGRectMake(0.0f, 0.0f, diameter, diameter);
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.clipsToBounds = YES;
        imageView.image = image;
        CALayer *layer = imageView.layer;
    
    
        layer.masksToBounds = YES;
    
        layer.cornerRadius =MAX( imageView.frame.size.height,imageView.frame.size.width)/2;
    
        UIGraphicsBeginImageContextWithOptions(imageView.bounds.size,NO, [UIScreen mainScreen].scale);
        CGContextRef context = UIGraphicsGetCurrentContext();
        [layer renderInContext:context];
    
    
        UIImage *roundedImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        return roundedImage;
    }
    

提交回复
热议问题