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

孤街醉人 提交于 2019-12-06 01:51:20
Shamsudheen TK

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

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