我的旋转图像没有锐利的边缘
我使用以下代码旋转我的图像:
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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用 UIImageView 的 CALayer 光栅化图像将提供最佳的抗锯齿效果。
Using the UIImageView's CALayer to rasterize the image will provide the best antialiasing.
图像本身的边缘看起来是锯齿状的,因为它们被直接放入像素网格中,而不是被插值。 最近邻插值是最简单的插值,如果你有像素网格 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.
只需在图像中添加 1px 透明边框即可
just add 1px transparent border to your image
每当您对图像进行变换(90° 增量除外)时,由于像素插值,都会导致边缘稍微变软。
Anytime you do transforms on an image (except in 90° increments) it is going to cause slightly softer edges due to pixel interpolation.
您可以在 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
最简单的解决方案:
https://stackoverflow.com/a/12066215/1469060
The simplest solution:
https://stackoverflow.com/a/12066215/1469060