简体   繁体   English

大小相等的UIImageView和UIImage

[英]UIImageView and UIImage of equal size

I came across a weird scenario in my code the other day: 前几天,我在代码中遇到了一个奇怪的场景:

I have a UIImageView that loads an image with setImage:. 我有一个UIImageView,它使用setImage:加载图像。 The UIImageView is prior initialized with a frame having the exact size of the image (41*41 px). UIImageView事先使用一个具有图像确切大小(41 * 41像素)的框架初始化。 I also set the content mode to UIViewContentModeCenter, which should ensure my image is never scaled. 我还将内容模式设置为UIViewContentModeCenter,这应确保我的图像永远不会缩放。

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). 相反,如果我使用额外的1个像素宽度初始化图像视图,则所有图像都可以正常工作(但是,现在的图像视图比图像宽1像素)。

Also if I initialize my UIImageView with initWithImage: that seems to work fine too. 另外,如果我使用initWithImage初始化UIImageView,那似乎也可以正常工作。 I checked this view's frame after initialization and I found it to be of the same size: 41*41. 初始化后,我检查了该视图的框架,发现它的大小相同: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: 我能想到的唯一解释是:

  1. There is a bug in the framework 框架中有一个错误
  2. 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. 我知道,例如,纹理始终是2的幂,尽管我怀疑这对于UIImageView是否具有重要意义。

For the record I'm compiling for OS 3.0 and the issue happens on both simulator and device. 出于记录,我正在为OS 3.0进行编译,该问题在模拟器和设备上均发生。

Is the UIImageView contained inside any other views? UIImageView是否包含在其他任何视图中? One thing I've had before is blurred controls caused by fractional pixel offsets. 我以前遇到过的一件事是由分数像素偏移引起的模糊控制。 ie 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. 即,如果您使用除法计算框架并以非整数float值结束,则UIKit可以在某些偏移和大小处添加一些奇怪的模糊。

So the image view may have an integer size, but it's absolute frame when taking the parent(s) into consideration may not be. 因此,图像视图可能具有整数大小,但考虑父对象时,它可能不是绝对帧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM