为什么PNG图像有时会变得模糊,具体取决于它在视图中的位置 [英] Why does a PNG image sometimes get blurry depending on its position in a view

查看:1617
本文介绍了为什么PNG图像有时会变得模糊,具体取决于它在视图中的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,当使用IB将PNG图像放入视图和/或将这些图像设置为视图周围的不同位置时,图像有时会出现轻微模糊。

I have noticed that when placing PNG images into a view using IB and/or animating those images to various positions around a view, the image can sometimes get a slight blur.

在大多数情况下,我可以通过在图像位置添加.5像素来弥补模糊。

In most cases I can remedy the blur by adding .5 of a pixel to the images position.

[lbLiteButton.layer setPosition:CGPointMake(140.5,159.5)];

有时我必须像上面一样调整x和y。有时我只需要调整x y。

Sometimes I have to adjust both x and y like above. Sometimes I only have to adjust x or y.

我记得在某处读到这与图像的大小和核心有关动画工作和半像素有关...但我无法在任何地方找到这篇文章!?

I remember reading somewhere that this has to do with the size of the image and how core animation works and something to do with half pixels... but I cant find the article anywhere!?

.5像素解决方案的问题在于每个PNG图像的大小都有所不同,因此你不能重复使用自定义动画,因为你必须自定义它适用于每个不同的图像。

The problem with the ".5 pixel" solution is that its different for every PNG image depending on size, so you can't reuse custom animation because you have to customise it for each different image.


  1. 有没有办法确保无论我在哪里放置或动画我的图像,我都不会得到任何模糊的职位?

  2. 有没有人对此有任何了解?

谢谢!

推荐答案

视图图层的位置属性基于其 anchorPoint 属性。默认情况下,即(0.5,0.5),表示图层的锚点位于其中心。如果您的视图(及其图层)是奇数个像素宽或高,设置位置的整数值将导致视图的原点不整合,从而导致您看到的模糊。

The position property of a view's layer is based on its anchorPoint property. By default, that is (0.5, 0.5), meaning that the anchor point of the layer is at its center. If your view (and its layer) are an odd number of pixels wide or high, setting an integral value for the position will cause the view's origin to be non-integral, leading to the blurriness you see.

要解决这个问题,您可以通过获取视图的所需中心位置,减去视图宽度的一半,舍入该值,然后添加视图的一半来找出位置的积分版本。宽度,并重复高度。您还可以将视图图层的 anchorPoint 设置为(0,0),并根据视图的来源定位视图。

To work around this, you could figure out an integral version of your position by taking the desired center position of the view, subtracting half of the view's width, rounding that value, then adding half of the view's width, and repeating for the height. You could also set the anchorPoint for your view's layer to (0,0) and position the view based on its origin.

这可能也与超级视图的错位有关。要诊断这一点,您可以使用Instruments中的Core Animation乐器,并选择Color Misaligned Images选项。它应该为应用程序中非像素对齐的任何视图或图层着色。

There's a chance that this might also be related to a misalignment of a superview. To diagnose this, you could use the Core Animation instrument in Instruments, and select the Color Misaligned Images option. It should color any views or layers that are non-pixel-aligned in your application.

这篇关于为什么PNG图像有时会变得模糊,具体取决于它在视图中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆