2

我只是在 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;
}

除非您仔细查看生成的图像,否则我看起来很好。边角不光滑。我怎样才能使角落更平滑/更柔软?

编辑:

石英圆角处理后的图像:

在此处输入图像描述

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

这是 UIImageView 的cornerRadius 版本更平滑。

在此处输入图像描述

问题在哪里?

4

1 回答 1

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;
}
于 2012-12-28T15:37:02.660 回答