石英圆角光滑的UIImage

Bor*_*zin 2 iphone smooth rounded-corners uiimage ios

我只是在测试下面的代码以在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;
}
Run Code Online (Sandbox Code Playgroud)

除非您仔细查看生成的图像,否则我看起来还不错。拐角不光滑。如何使拐角更光滑/更柔软?

编辑:

石英圆角处理后的图像:

在此处输入图片说明

就像你可以看到角落不光滑。

这个版本带有UIImageView的cornerRadius,更加平滑。

在此处输入图片说明

渔获物在哪里?

Jee*_*ton 5

这是UIImage类别:

- (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;
}
Run Code Online (Sandbox Code Playgroud)

  • 您应该使用以下方法更新答案:UIGraphicsBeginImageContextWithOptions(self.size,NO,0.0); (3认同)