I have 2 images, one in portrait mode, and the other in landscape mode. What is the best way to switch these images when a mobile device view rotation takes place?
Currently I just display the portrait image. And when the device rotates to landscape mode the portrait image is simply stretched.
Should I be checking within the orientation rotation handler and simply reset the image to the proper orientational image (i.e. set it manually based on the orientation)??
Thanks!
I found three ways.I think the last one is better
1: Autoresizing
Example:
UIImageView *myImageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourImage.png"]];
myImageView.frame = self.view.bounds;
myImageView.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight
myImageView.contentMode = UIViewContentModeScaleAspectFill;
[self.view addSubview:myImageView];
[imageView release];
2: CGAffineTransformMakeRotation
Example:
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration {
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
myImageView.transform = CGAffineTransformMakeRotation(M_PI / 2);
}
else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight){
myImageView.transform = CGAffineTransformMakeRotation(-M_PI / 2);
}
else {
myImageView.transform = CGAffineTransformMakeRotation(0.0);
}
}
3:Set autosizing of myImageView as auto fill the screen in Interface Builder
Example:
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
if((self.interfaceOrientation == UIDeviceOrientationLandscapeLeft) || (self.interfaceOrientation == UIDeviceOrientationLandscapeRight)){
myImageView.image = [UIImage imageNamed:@"myImage-landscape.png"];
} else if((self.interfaceOrientation == UIDeviceOrientationPortrait) || (self.interfaceOrientation == UIDeviceOrientationPortraitUpsideDown)){
myImageView.image = [UIImage imageNamed:@"myImage-portrait.png"];
} }
see more solutions here
developer.apple solution is here