Scale image to fit screen on iPhone rotation

2019-02-09 07:30发布

I have a UIScrollView containing an UIImageView and I have some trouble to get the correct behavior when the iPhone rotates.

Goal: I am trying to obtain the following when going from portrait to landscape:

_________
|AAAAAAA|
|BBBBBBB|         _________________
|CCCCCCC|         |     AAAAAA     |
|DDDDDDD|    -->  |     CCCCCC     |
|EEEEEEE|         |     EEEEEE     |
|FFFFFFF|         |_____GGGGGG_____|
|GGGGGGG|
---------

Here the entire image in the portrait view is scaled to fit in the landscape view when the iPhone rotates. It is also centered. I am also trying to preserve the aspect ratio. User interaction is also on, and the user should be able to use the entire screen to pan/zoom the image.

Currently I have the following autoresizingMask on the scrollView:

 scrollView.autoresizingMask =(UIViewAutoresizingFlexibleWidth |
                               UIViewAutoresizingFlexibleHeight);       

But this gives the following

_________
|AAAAAAA|
|BBBBBBB|         _________________
|CCCCCCC|         |AAAAAA          |
|DDDDDDD|    -->  [BBBBBB          |
|EEEEEEE|         [CCCCCC          |
|FFFFFFF|         [DDDDDD__________|
|GGGGGGG|
---------

This setting preserves scale and offset from upper left corner.

Question: Is it possible to obtain this behaviour using suitable autoresizingMask? If not, one should probably set

 scrollView.autoresizingMask = UIViewAutoresizingNone;

and manually set zoomScale and contentOffset for the UIScrollView on rotation. But, where should that be done? What about animating that change?

Solution: By very slightly modifying the answer below I got the above behaviour using the below code:

imageView.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
                                     UIViewAutoresizingFlexibleHeight);

scrollView.autoresizingMask =(UIViewAutoresizingFlexibleWidth 
                                     | UIViewAutoresizingFlexibleHeight);

imageView.contentMode = UIViewContentModeScaleAspectFit;
scrollView.contentMode = UIViewContentModeCenter;

2条回答
Animai°情兽
2楼-- · 2019-02-09 07:36

Try this:

scrollView.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
                           UIViewAutoresizingFlexibleHeight);    
imageView.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
                           UIViewAutoresizingFlexibleHeight);

imageView.contentMode = UIViewContentModeAspectFit; // You could also try UIViewContentModeCenter

I'm not sure if the imageView will get automatically resized within a UIScrollView, so if the autoresizing doesn't work, you could try setting the frame yourself on rotate to equal the size of the scrollview.

查看更多
混吃等死
3楼-- · 2019-02-09 07:49

I had a similar situation, a very simple scrollView with 3 imageViews ('previous', 'current', and 'next') to display a series of photos.

The photos were all sized for the iPad, but a mix of landscape and portrait orientations. I wanted them to fit into the screen, (i.e. letterboxed if necessary), with aspect ratio retained. I found I only needed to do this to the 3 imageViews for it to work:

imageView.contentMode = UIViewContentModeScaleAspectFit;
查看更多
登录 后发表回答