landscape mode images in a full screen UIImageView

我只是一个虾纸丫 提交于 2019-12-06 18:50:25

Just set this:

imageView.contentMode = UIViewContentModeAspectFit;

And it will keep the proportion and fit the screen, not filling it.

You might want to change the UIImageView's contentMode property according to the ratio of the picture.

So in viewDidLoad :

if (imageView.image.size.height > imageView.image.size.width) { 
    imageView.contentMode = UIViewContentModeAspectFill;
} else { 
    imageView.contentMode = UIViewContentModeAspectFit; 
}

and change that mode when the user switches to landscape, so for instance in the willAnimateRotationToInterfaceOrientation:duration: :

if (UIInterfaceOrientationIsLandscape(toIntefaceOrientation)){
   if (imageView.image.size.height > imageView.image.size.width) { 
       imageView.contentMode = UIViewContentModeAspectFit;
   } else { 
       imageView.contentMode = UIViewContentModeAspectFill; 
   }
} else {
   if (imageView.image.size.height > imageView.image.size.width) { 
       imageView.contentMode = UIViewContentModeAspectFill;
   } else { 
       imageView.contentMode = UIViewContentModeAspectFit; 
   }
}

NB : note tested, but might work

UIImageView *imgView;
imgView.contentMode = UIViewContentModeCenter;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!