UIImage with smooth rounded corners in quartz

I just check the following code for round corners on a UIImage:

- (UIImage *)makeRoundedImage:(UIImage *)image radius:(float)radius; { CALayer *imageLayer = [CALayer layer]; imageLayer.frame = CGRectMake(0, 0, image.size.width, image.size.height); imageLayer.contents = (id) image.CGImage; imageLayer.masksToBounds = YES; imageLayer.cornerRadius = radius; UIGraphicsBeginImageContext(image.size); [imageLayer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *roundedImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return roundedImage; } 

I look great if you don’t look at the generated image. Corners are not smooth. How to make corners smoother / softer?

EDIT:

Processed image with rounded corners in quartz:

enter image description here

As you can see, the angle is not smooth.

This is the version with the UIImageView cornerRadius, which is much smoother.

enter image description here

Where to catch?

+1
source share
1 answer

Here is the UIImage category:

 - (UIImage *) imageWithRoundedCornersRadius: (float) radius { // Begin a new image that will be the new image with the rounded corners UIGraphicsBeginImageContextWithOptions(self.size, NO, 0); // Add a clip before drawing anything, in the shape of an rounded rect CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height); [[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius] addClip]; // Draw your image [self drawInRect:rect]; // Get the image, here setting the UIImageView image UIImage *roundedImage = UIGraphicsGetImageFromCurrentImageContext(); // Lets forget about that we were drawing UIGraphicsEndImageContext(); return roundedImage; } 
+4
source

All Articles