我的旋转图像没有锐利的边缘

发布于 2024-07-30 02:21:29 字数 283 浏览 1 评论 0原文

我使用以下代码旋转我的图像:

CGAffineTransform rotate = CGAffineTransformMakeRotation( [ratio floatValue] );
[imageView setTransform:rotate];

但它没有锋利的边缘,有人知道这个问题的解决方案吗?

这是我得到的图像:

在此处输入图像描述

I am rotating my image with the following code:

CGAffineTransform rotate = CGAffineTransformMakeRotation( [ratio floatValue] );
[imageView setTransform:rotate];

But it doesn't have sharp edges, does someone know a solution for this?

Here's the image I get:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

玩套路吗 2024-08-06 02:21:29

使用 UIImageView 的 CALayer 光栅化图像将提供最佳的抗锯齿效果。

imageView.layer.shouldRasterize = YES;

Using the UIImageView's CALayer to rasterize the image will provide the best antialiasing.

imageView.layer.shouldRasterize = YES;
2024-08-06 02:21:29

图像本身的边缘看起来是锯齿状的,因为它们被直接放入像素网格中,而不是被插值。 最近邻插值是最简单的插值,如果你有像素网格 A 和您将图像移动到像素网格 B,只需从网格 A 中选择最接近的像素即可选择网格 B 中的像素。其他形式的插值选择最接近像素的加权平均值以得出网格 B 中的像素值。

您的图像具有锯齿状边缘,看起来像是使用最近邻插值,这可能是 iPhone 上仿射变换的默认插值类型。

当您使用除最近邻之外的其他插值方案时,您将获得锯齿效果,其中当您从一个像素网格转移到另一个像素网格时,子采样并不完美。 这种效果使图像本身的边缘看起来比其他情况更模糊。

The edges of the image itself look jagged because they are being placed into a pixel grid directly, and not being interpolated. Nearest Neighbor Interpolation is the simplest kind of interpolation, where if you have pixel grid A and you move your image to pixel grid B, the pixels in grid B are chosen by simply choosing the closest pixel from grid A. Other forms of interpolation choose a weighted average of the closest pixels to arrive at the pixel value in grid B.

Your image, with its jagged edges, looks like it's using nearest neighbor interpolation, which may be the default type of interpolation on an affine transform on an iphone.

When you use some other interpolation scheme other than nearest neighbor, you'll get aliasing effects, where the subsampling isn't perfect as you transfer from one pixel grid to another. That effect makes edges in the image itself seem blurrier than they otherwise would.

红衣飘飘貌似仙 2024-08-06 02:21:29

只需在图像中添加 1px 透明边框即可

CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);
UIGraphicsBeginImageContext( imageRect.size ); 
[image drawInRect:CGRectMake(1,1,image.size.width-2,image.size.height-2)];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

just add 1px transparent border to your image

CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);
UIGraphicsBeginImageContext( imageRect.size ); 
[image drawInRect:CGRectMake(1,1,image.size.width-2,image.size.height-2)];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
最美的太阳 2024-08-06 02:21:29

每当您对图像进行变换(90° 增量除外)时,由于像素插值,都会导致边缘稍微变软。

Anytime you do transforms on an image (except in 90° increments) it is going to cause slightly softer edges due to pixel interpolation.

甜妞爱困 2024-08-06 02:21:29

您可以在 Info.plist 中设置一个启用边缘抗锯齿的键:UIViewEdgeAntialiasing。

如以下答案所述: 何时我使用变换属性旋转 UIImageView,边缘像素化

There is a key that you can set in Info.plist that enables antialiasing of the edges: UIViewEdgeAntialiasing.

As described in the following answer: When I rotate an UIImageView with the transform property, the edges pixellate

过去的过去 2024-08-06 02:21:29

最简单的解决方案:

只需将此键值对添加到您的 Info.plist 中即可:
UIViewEdgeAntialiasing 设置为 YES。

https://stackoverflow.com/a/12066215/1469060

The simplest solution:

Simply add this key-value pair to your Info.plist:
UIViewEdgeAntialiasing set to YES.

https://stackoverflow.com/a/12066215/1469060

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文