【发布时间】:2009-08-01 18:19:23
【问题描述】:
我正在使用以下代码旋转我的图像:
CGAffineTransform rotate = CGAffineTransformMakeRotation( [ratio floatValue] );
[imageView setTransform:rotate];
但它没有锋利的边缘,有人知道解决方案吗?
这是我得到的图像:
【问题讨论】:
标签: iphone image uiimageview rotation
我正在使用以下代码旋转我的图像:
CGAffineTransform rotate = CGAffineTransformMakeRotation( [ratio floatValue] );
[imageView setTransform:rotate];
但它没有锋利的边缘,有人知道解决方案吗?
这是我得到的图像:
【问题讨论】:
标签: iphone image uiimageview rotation
使用 UIImageView 的 CALayer 栅格化图像将提供最佳的抗锯齿效果。
imageView.layer.shouldRasterize = YES;
【讨论】:
图像本身的边缘看起来参差不齐,因为它们被直接放置到像素网格中,而不是被插值。 Nearest Neighbor Interpolation 是最简单的插值类型,如果您有像素网格 A,并且您将图像移动到像素网格 B,则只需从网格 A 中选择最接近的像素即可选择网格 B 中的像素。其他形式的插值选择获得网格 B 中像素值的最近像素的加权平均值。
您的图像带有锯齿状边缘,看起来像是在使用最近邻插值,这可能是 iphone 仿射变换的默认插值类型。
当您使用除最近邻之外的其他插值方案时,您将获得aliasing 效果,当您从一个像素网格转移到另一个像素网格时,子采样并不完美。这种效果使图像本身的边缘看起来比其他情况下更模糊。
【讨论】:
只需为您的图像添加 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();
【讨论】:
每当您对图像进行变换(90° 增量除外)时,由于像素插值,它会导致稍微柔和的边缘。
【讨论】:
您可以在 Info.plist 中设置一个启用边缘抗锯齿功能的键:UIViewEdgeAntialiasing。
如以下答案所述:When I rotate an UIImageView with the transform property, the edges pixellate
【讨论】:
最简单的解决方案:
只需将此键值对添加到您的 Info.plist 中: UIViewEdgeAntialiasing 设置为 YES。
【讨论】: