开发者

UIScrollView zoomscale inaccuracy

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 res开发者_JAVA百科ult 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:

  1. Is the algorithm above the right way to get what I want?
  2. If yes, is there a way to take the inaccuracy into account, i.e. a better algorithm?

Thanks for any reply!


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:

(currentImage.size.width+frameWidth-1)/currentImage.size.width

This will give you a whole number rounded up every time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜