将其剪裁成圆形后的低质量UIImage

时间:2015-02-18 11:26:14

标签: ios xcode uiimage

我正在尝试剪辑UIImage以使其成为圆形,我开始使用140 * 140px图像,然后运行此代码:

 //round the image

    UIImageView *roundView = [[UIImageView alloc] initWithImage:smallImage];
    UIGraphicsBeginImageContextWithOptions(roundView.bounds.size, NO, 1.0);
    [[UIBezierPath bezierPathWithRoundedRect:roundView.bounds
                                cornerRadius:roundView.frame.size.width/2] addClip];
    [smallImage drawInRect:roundView.bounds];
    UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    smallImage = finalImage;

    //end round image

这可以按照需要工作,但质量非常低,图像看起来很模糊,圆圈周围的边缘是锯齿状的。我想实现它们同样的影响:

image.layer.cornerRadius = self.thumbnailView.frame.size.width / 2;
image.clipsToBounds = YES;

不确定为什么图像质量如此之低。有人可以给我一些指示吗?

3 个答案:

答案 0 :(得分:5)

您可能希望将比例保持在0.f,因此它与设备比例(视网膜/非视网膜)相匹配。

UIGraphicsBeginImageContextWithOptions(roundView.bounds.size, NO, 0.f);

你也可以画一个这样的圆圈:

UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.f);
CGRect interiorBox = CGRectInset(rect, 0.f, 0.f);
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithOvalInRect:insideBox];
[bezierPath addClip];
[image drawInRect:rect];
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

答案 1 :(得分:2)

您将错误的比例传递给UIGraphicsBeginImageContextWithOptions。您正在通过1.使用UIGraphicsBeginImageContextWithOptions(roundView.bounds.size, NO, [UIScreen mainScreen].scale);

答案 2 :(得分:0)

你确定你真的需要对实际图像这样做吗?仅仅将图像视图四舍五入是不够的?

你可以很容易地做到这一点。

UIImageView *imageView = // your image view with the image
imageView.frame = CGRectMake(0, 0, 140, 140); // the size you want
imageView.clipsToBounds = YES;
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.layer.cornerRadius = 70; // make the corner radius half of the size

这将显示图像视图裁剪成圆圈的图像。

图像保持不变。它只是圆形图像的显示。

它也便宜得多(时间和记忆)。

在表格中执行此操作不会减慢速度。您只需为每个出列的单元格执行一次此操作。对于重用的单元格,您不需要像第一次创建单元格时那样执行此操作。

相关问题