【问题标题】:UIImage with smooth rounded corners in quartz石英中具有光滑圆角的 UIImage
【发布时间】:2013-08-08 19:48:15
【问题描述】:

我只是在 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 更平滑的版本。

问题在哪里?

【问题讨论】:

  • 您能否发布您要裁剪的图像以及结果如何?
  • 我不想将圆角应用到 UIImageView 因为我想将此图像保存到文件系统。
  • 我刚刚更新了问题...
  • 将 UIGraphicsBeginImageContext 更改为 UIGraphicsBeginImageContextWithOptions(image.size, NO, [[UIScreen mainScreen] scale]);
  • UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);会有同样的效果

标签: iphone ios uiimage rounded-corners smooth


【解决方案1】:

这里是 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;
}

【讨论】:

  • 我也试过这个,但它是一样的。问题已通过示例更新。请看...
  • 你应该用这个更新你的答案: UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);
猜你喜欢
  • 2017-07-26
  • 1970-01-01
  • 2023-01-30
  • 1970-01-01
  • 2022-01-12
  • 2010-09-20
  • 2010-09-17
  • 2011-06-20
相关资源
最近更新 更多