【问题标题】:Low quality UIImage after clipping it to a circle将其裁剪为圆形后的低质量 UIImage
【发布时间】:2015-02-18 19:45:09
【问题描述】:

我正在尝试剪辑 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;

不知道为什么图片质量这么低。有人可以给我一些指点吗?

【问题讨论】:

  • 剪裁你的 UIImage 使其成为圆形是可取的..??您可以更改圆形图像视图,而不是在 imageView 中添加您的图像..
  • 我以前也这样做过,但我在 tableview 中有很多 UIImage,所以四舍五入的图像视图会大大降低性能。
  • @Kex 对我来说听起来你是在 cellForRowAtIndexPath 方法中执行此操作。这样做是错误的。只有少数细胞被创建然后出队。您需要为每个创建的单元执行一次此操作。那么它就已经有了圆角,以备将来重复使用。

标签: ios xcode uiimage


【解决方案1】:

您可能希望将比例保持在 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();

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      您确定确实需要对实际图像执行此操作吗?只是让图像视图变圆还不够吗?

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

      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
      

      这将显示您的图像被图像视图裁剪成一个圆圈。

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

      它也便宜得多(时间和内存)。

      在表格中执行此操作不会减慢速度。您只需为每个出列单元格执行一次。对于重复使用的单元格,您不需要这样做,因为它是在首次创建单元格时完成的。

      【讨论】:

      • Asker 写道:“我想实现与 'image.layer.cornerRadius' 相同的效果”,所以他似乎知道这一点,他需要更改实际图像。无论如何,它可能对其他来这里的人有所帮助。
      • @Vive 啊,哈哈!我错过了。好吧,从 OP 的评论来看,他似乎错误地使用了出列单元格,这就是他表现不佳的原因。所以问题是这样的。
      • 不,不是单元格,上周表格存在大量问题,最终问题在于更改所有图像视图图层的角半径。如果使用很多,似乎会极大地影响表格的滚动性能。
      • @Fogmeister。我很想试试你刚刚写的东西,看看它是否有效。也许是因为即使重复使用了一个单元格,我也会继续这样做。有没有办法从 FriendViewCell *cell = (FriendViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath] 中找出一个单元格是否是重用单元格?
      • 哦,这很有意义。 prepareForReuse 听起来以后也会对我有所帮助。我会试一试。谢谢
      猜你喜欢
      • 1970-01-01
      • 2019-08-08
      • 2011-09-25
      • 2013-07-27
      • 2011-09-19
      • 1970-01-01
      • 1970-01-01
      • 2019-10-27
      • 2017-06-13
      相关资源
      最近更新 更多