问题
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