UIImage,石英光滑圆角

时间:2012-12-28 14:49:06

标签: iphone ios uiimage rounded-corners smooth

我只是测试以下代码来绕过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;
}

除非你密切关注生成的图像,否则我看起来很好。角落不顺畅。如何使角落变得更光滑/更柔和?

编辑:

在石英中加工圆角的图像:

enter image description here

就像你可以看到角落不顺畅。

这是UIImageView的cornerRadius更加平滑的版本。

enter image description here

渔获物在哪里?

1 个答案:

答案 0 :(得分: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;
}