UIImageView 和 UIImage 大小相等
前几天我在代码中遇到了一个奇怪的场景:
我有一个 UIImageView 使用 setImage: 加载图像。 UIImageView 预先使用具有图像精确尺寸 (41*41 px) 的框架进行初始化。我还将内容模式设置为 UIViewContentModeCenter,这应该确保我的图像永远不会缩放。
现在,当我查看图像视图时,我发现图像在水平方向上有一些裁剪,并且看起来有点模糊(这是一个明显的迹象,表明背景中正在发生一些重新缩放)。相反,如果我用 1 个额外像素宽度初始化图像视图,那么一切都会完美运行(但是我的图像视图现在比我的图像宽 1 个像素)。
另外,如果我用 initWithImage: 初始化 UIImageView ,那似乎也可以正常工作。我在初始化后检查了这个视图的框架,发现它的大小相同:41*41。
所以底线是我有一些解决这个问题的方法,但我试图了解这里发生的事情。我能想到的唯一解释是:
- 框架中有一个错误
- 图像的渲染对于特定尺寸的图像效果不佳。我知道例如纹理总是 2 的幂,尽管我怀疑这对于 UIImageView 有多大意义。
根据记录,我正在为 OS 3.0 进行编译,并且该问题发生在模拟器和设备上。
I came across a weird scenario in my code the other day:
I have a UIImageView that loads an image with setImage:. The UIImageView is prior initialized with a frame having the exact size of the image (41*41 px). I also set the content mode to UIViewContentModeCenter, which should ensure my image is never scaled.
Now when I look at my image view I see that the image is a bit cropped horizontally and it appears a little blurred (this is a telltale sign that some rescaling is happening in the background). If instead I initialize my image view with 1 extra pixel in width, all works perfectly (however my image view is now one pixel wider than my image).
Also if I initialize my UIImageView with initWithImage: that seems to work fine too. I checked this view's frame after initialization and I found it to be of the same size: 41*41.
So bottom line is I have a few workarounds for this issue, but I'm trying to understand what's happening here. The only explanations I can think of are:
- There is a bug in the framework
- The rendering of images doesn't work well for images of a particular size. I know that for examples textures are always powers of 2, though I doubt this has much significance for UIImageView.
For the record I'm compiling for OS 3.0 and the issue happens on both simulator and device.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
UIImageView 是否包含在任何其他视图中?我以前遇到过的一件事是由分数像素偏移引起的模糊控制。即,如果您使用除法计算帧并最终得到非整数浮点值,则 UIKit 可以在某些偏移量和大小处添加一些奇怪的模糊。
因此,图像视图可能具有整数大小,但考虑到父级时它可能不是绝对框架。
Is the UIImageView contained inside any other views? One thing I've had before is blurred controls caused by fractional pixel offsets. i.e. if you calculate a frame using division and end up with a non-integer float value then UIKit can add some strange blurring at certain offsets and sizes.
So the image view may have an integer size, but it's absolute frame when taking the parent(s) into consideration may not be.