What is the proper way to handle image rotations for iphone/ipad?

我的梦境 提交于 2019-12-07 16:38:23

问题


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!


回答1:


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



来源:https://stackoverflow.com/questions/11733279/what-is-the-proper-way-to-handle-image-rotations-for-iphone-ipad

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