Making a UIImage to a circle form

后端 未结 10 2022
南旧
南旧 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:45

    Sorry for ugly formatting. Better version of aToz's answer.

    @interface UIImage (CircleMask)
    
    + (UIImage *)roundedRectImageFromImage :(UIImage *)image
                                      size :(CGSize)imageSize
                          withCornerRadius :(float)cornerRadius;
    
    @end
    
    @implementation UIImage(CircleMask)
    
    +(UIImage*)roundedRectImageFromImage:(UIImage *)image
                                    size:(CGSize)imageSize
                        withCornerRadius:(float)cornerRadius
    {
        UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);   //  <= notice 0.0 as third scale parameter. It is important cause default draw scale ≠ 1.0. Try 1.0 - it will draw an ugly image..
        CGRect bounds = (CGRect){CGPointZero,imageSize};
        [[UIBezierPath bezierPathWithRoundedRect:bounds
                                    cornerRadius:cornerRadius] addClip];
        [image drawInRect:bounds];
        UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        return finalImage;
    }
    
    @end
    

    The same thing in Swift:

    extension UIImage{
    
        class func roundedRectImageFromImage(image:UIImage,imageSize:CGSize,cornerRadius:CGFloat)->UIImage{
            UIGraphicsBeginImageContextWithOptions(imageSize, false, 0.0)
            let bounds = CGRect(origin: CGPointZero, size: imageSize)
            UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius).addClip()
            image.drawInRect(bounds)
            let finalImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return finalImage
        }
        
    }
    
    0 讨论(0)
  • try this

    UIImageView *circleImageview=[[UIImageView alloc]init];
    circleImageview.frame=CGRectMake(0,10, 80, 80)]; 
    circleImageview.layer.cornerRadius=circleimagview.frame.size.height/2;
     circleImageview.userInteractionEnabled=YES;
     [self.view addSubview:circleImageview];
    

    Note:Make Sure height and width will be same of that view.

    0 讨论(0)
  • 2020-12-02 17:46

    I resolve my problem with UIBezierPath

    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:yourImageView.bounds];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.path = path.CGPath;
    yourImageView.layer.mask = maskLayer;
    
    0 讨论(0)
  • 2020-12-02 17:47

    This might help you, pass corner radius as half of the reference view where you want to display the image or you can set a custom rect,

    eg. In my case I want to display the image on a "imageView", so the corner radius in this case would be imageView.frame.size.width/2

    - (void)displayImage
    {
        imageView.image = [self getRoundedRectImageFromImage:@"Test.png" onReferenceView:imageView withCornerRadius: imageView.frame.size.width/2];
    }
    
    
    
    - (UIImage *)getRoundedRectImageFromImage :(UIImage *)image onReferenceView :(UIImageView*)imageView withCornerRadius :(float)cornerRadius
    {
        UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 1.0);
        [[UIBezierPath bezierPathWithRoundedRect:imageView.bounds
                                    cornerRadius:cornerRadius] addClip];
        [image drawInRect:imageView.bounds];
        UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        return finalImage;
    }
    
    0 讨论(0)
提交回复
热议问题