landscape mode images in a full screen UIImageView

a 夏天 提交于 2019-12-23 03:38:19

问题


Is it possible to let the UIImageView handle landscape images differently from portrait images?

I'll try to explain, I have ImageViewController with a UIImageView that fill the entire screen.
In some point the user launch a UIImagePickerController and choose a picture from the camera roll.
Then I create an instance of the ImageViewController with that picture and push it

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];


    ImageViewController *ivc = [[ImageViewController alloc] initWithImage:image];
    [picker pushViewController:ivc animated:YES];
    [ivc release];
}

The problem is, if the image is in landscape mode, it still fill the entire screen.
I want it to look like how the iPhone photos app present a landscape image (in the center of the screen with a black background).

Is there a build in way to show landscape mode images in UIImageView? or do I need to set the imageView frame according to the image orientation?


回答1:


Just set this:

imageView.contentMode = UIViewContentModeAspectFit;

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




回答2:


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




回答3:


UIImageView *imgView;
imgView.contentMode = UIViewContentModeCenter;


来源:https://stackoverflow.com/questions/11560711/landscape-mode-images-in-a-full-screen-uiimageview

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