UIScrollView缩放比例不准确
我正在尝试使用 UIZoomView 将图像放入框架(不是 view.frame)的预定义图形表示中。 为了以图像适合所需帧宽度 250 的方式调整缩放比例,代码基本上是:
float frameWidth=250;
float currentZoomScale=frameWidth/currentImage.size.width;
self.scrollView.zoomScale=currentZoomScale;
这几乎可以正常工作......几乎。我的问题是根据图像宽度略有不准确。 例如,宽度为 640 的图像将导致 ZoomScale 为 0.390625。 但屏幕上的可见图像宽度将比 250 低 1 像素。对于不同尺寸的其他图像,该算法有效。
我怀疑原因是除法结果的浮点性质与实际屏幕像素的整数性质发生冲突...我的意思是缩放比例应该类似于 0.391 或类似的值(我尝试过 0.4,太大了)。
我的问题:
- 上面的算法是否正确 得到我想要的东西?
- 如果是,有没有办法考虑到不准确性,即更好的算法?
感谢您的任何回复!
I am trying to fit an image into a predefined graphical representation of a frame (not a view.frame) using UIZoomView.
For adjusting the zoomscale in a way that the image fits into the desired frame width of 250, the code basically is:
float frameWidth=250;
float currentZoomScale=frameWidth/currentImage.size.width;
self.scrollView.zoomScale=currentZoomScale;
This works almost fine...almost. My problem is a slight inaccuracy depending on the image width.
For example, an image with a width of 640 will result in a zoomScale of 0.390625.
But the visible image width on the screen will be 1 pixel below 250. With other images of different sizes the algorithm works.
I suspect the reason is that the floating point nature of the division result collides with the integer nature of the actual screen pixels...I mean that the zoomscale should be something like 0.391 or similar (I tried 0.4, which is too big).
My questions:
- Is the algorithm above the right way
to get what I want? - If yes, is there a way to take the inaccuracy into account, i.e. a better algorithm?
Thanks for any reply!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑您使用的除法会产生一个十进制数,当转换为像素时,十进制数会被废弃,因为您不能拥有 0.5 个像素。您可以自动四舍五入。将您的算法更改为:
这将为您每次四舍五入得到一个整数。
I suspect the division you are using is producing a decimal number and when converted to pixels the decimal number is scrapped because you can't have 0.5 of a pixel. You could automatically round up. Change you algorithm to this:
This will give you a whole number rounded up every time.