Should We use 2 different Images or Single Image for Landscape and Portrait Mode

北战南征 提交于 2019-12-23 03:32:29

问题


I am doing a app for iPad in which we have added two images one for Landscape and the other for portrait mode, I have written code for.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {  
// in this method we have called landscape and portrait frames basing on the
// status bar rotation if it is turned to landscape then the landscape image
// is called and the textfields frames are set else portrait image is set and
// then the textfield frames are set on to it.  
}  

Can somebody help me out by saying is the right way of using 2 different images, or we should use only 1 image for both landscape and portrait.If so, how can we use those only single image? My app is crashing due to this. Kindly do help me.

Thanks in advance.


回答1:


There is no harm in using two different images it just increases the size of the image bundle.. i have done this for an app on appstore.. an MNC's banks ipad app. So no harm go ahead and use it. I personally it is better this way than to process the image and resize it.




回答2:


Hello kinthali As per the documentation

Handling View Rotations

By default, the UIViewController class displays views in portrait mode only. To support additional orientations, you must override the shouldAutorotateToInterfaceOrientation: method and return YES for any orientations your subclass supports. If the autoresizing properties of your views are configured correctly, that may be all you have to do. However, the UIViewController class provides additional hooks for you to implement additional behaviors as needed.

To temporarily turn off features that are not needed or might otherwise cause problems during the orientation change, you can override the willRotateToInterfaceOrientation:duration: method and perform the needed actions there. You can then override the didRotateFromInterfaceOrientation: method and use it to reenable those features once the orientation change is complete.

As per your question all you need to do is to return Yes for supported orientation in shouldAutorotateToInterfaceOrientation and you can call to a function to set the background image (Two different images for Portrait and landscape orientation)as follows

[self updateBackgroundImageForOrientation:PROBABLE_INTERFACE_ORIENTATION];

In the method implementation you can update the background image.This way you can implement the desired behavior. Since you have not actually posted any functional code it is hard to point out the cause of the crash. You can debug and see the console logs for root cause of the crash.




回答3:


You are probably best having two images and you can do the following:

EDIT: I've added some code which will make sure that your image fills the entire space available.

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
if((self.interfaceOrientation == UIDeviceOrientationLandscapeLeft) || (self.interfaceOrientation == UIDeviceOrientationLandscapeRight)){

[landscapeimage setHidden:NO];
[portraitimage setHidden:YES];

CGRect landscapeframe = landscapeimage.frame;
landscapeframe.origin.x = 0; // new x coordinate
landscapeframe.origin.y = 0; // new y coordinate
landscapeframe.size.width = 1024;
lanscapeframe.size.height = 768;

}

else if((self.interfaceOrientation == UIDeviceOrientationPortrait) || (self.interfaceOrientation == UIDeviceOrientationPortraitUpsideDown)){

[landscapeimage setHidden:YES];
[portraitimage setHidden:NO];

CGRect portraitframe = portraitimage.frame;
portraitframe.origin.x = 0; // new x coordinate
portraitframe.origin.y = 0; // new y coordinate
portraitframe.size.width = 768;
portraitframe.size.height = 1024;

}

Hope this helps you!



来源:https://stackoverflow.com/questions/8374679/should-we-use-2-different-images-or-single-image-for-landscape-and-portrait-mode

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