UIImage,石英光滑圆角 [英] UIImage with smooth rounded corners in quartz

查看:119
本文介绍了UIImage,石英光滑圆角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在UIImage上测试以下代码以圆角:

   - (UIImage *)makeRoundedImage:(UIImage * )图像半径:(浮点)半径; 
{
CALayer * imageLayer = [CALayer图层];
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();

返回roundedImage;
}

除非仔细观察生成的图像,否则我看起来很好。角落不顺畅。如何使角落变得更平滑/更柔和?



编辑:



石英圆角加工图像:





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



这是UIImageView的cornerRadius的版本更顺畅。



<这是UIImage类别:

/ p>

   - (UIImage *)imageWithRoundedCornersRadius :( float)radius 
{
//开始一个新图像将是具有圆角的新图像
UIGraphicsBeginImageContextWithOptions(self.size,NO,0);

//在绘制任何东西之前添加一个剪辑,形状为圆角矩形
CGRect rect = CGRectMake(0,0,self.size.width,self.size.height);
[[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius] addClip];

//画你的形象
[self drawInRect:rect];

//获取图像,此处设置UIImageView图像
UIImage * roundedImage = UIGraphicsGetImageFromCurrentImageContext();

//让我们忘记我们正在绘制
UIGraphicsEndImageContext();

返回roundedImage;
}


I am just testing the following code to round corners on 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 looks fine unless you look closely to generated image. Corners are not smooth. How can I make corners to be smoother/softer ?

EDIT:

Processed image with rounded corners in quartz:

Like you can see corner is not smooth.

This is version with UIImageView's cornerRadius that is much more smoother.

Where is the catch ?

解决方案

Here is 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;
}

这篇关于UIImage,石英光滑圆角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆