While developing an iOS Application, you may need to position objects on the imageView dynamically based on the screen size, especially if the imageView size changes based on the device orientation. If you want to locate objects based on x and y coordinations on the imageView with Aspect Fit (which changes imageView size to fit the screen) you need to reposition them due to expanded or shrunk imageView.

As you know, the Aspect Fit setting of the imageView expands or shrinks the image to fit the width or height of the screen. Therefore, x and y coordinations of the objects on the imageView should be repositioned based on Aspect Ratio of the imageView. Let's assume that you have a 1600px (width) X 2005px (height) image on your imageView, and you get (30.0, 40.0) coordination from database for locating the object. If your imageView size is 768px W X 1004px H in the portrait mode, the image size will be 768px W X 962px H to fit the screen. In this case, the Aspect ratio is 0.48. Therefore, the x and y coordination should be (14.4, 19.2). Pretty simple math so far, isn't it?

Now, here is the issue. You are not able to get resized width and height from any property. You may try to get imageView.image.origin.size.width and height to get width and height of the resized image of the imageView. Unfortunately, imageView.image.origin.size.width and height return the original image's width and height which are 1600px W and 2005px H. I tried to find reference of the resized image object of the imageView, but there is no copied image object for the resized image. Therefore, you have to get the resized image's width and height based on Aspect ratio. Unfortunately, also there is no way to get current Aspect ratio based on the screen size or device orientation.

Let me explain how to get current Aspect ratio of the imageView:

As you know, Aspect fit will expand or shrink the imageView to fit width or height of the screen, and both width and height should fit on the screen. For the above case, reducing ratio of the width is 768 / 1600 = 0.48 and the ratio of the height is 1004 / 2005 = 0.50. To fit both width and height in the screen, lower aspect ratio should be applied for both, so 0.48 ratio will be used in this case. Lastly, you need to apply this calculation whenever the device changes its orientation to get dynamic position of x and y coordination. In addition, if you create a custom imageView and put this implementation to layoutSubviews(), objects will be repositioned without moving animation.

Read Next
Appnovation Blog Default Header

Give Alfresco a RESTful webscript

01 May, 2012|4 min