Dans l'application iPhone, lors de l'exécution de l'application sur l'appareil Comment détecter la résolution d'écran de l'appareil sur lequel l'application s'exécute?
Réponses
Trop de publicités?CGRect screenBounds = [[UIScreen mainScreen] bounds];
Qui va vous donner l'intégralité de la résolution de l'écran dans les points, de sorte qu'il serait plus généralement 320x480 pour les iPhones. Même si l'iPhone4 est beaucoup plus grande taille d'écran iOS donne encore de retour 320x480 au lieu de 640x960. C'est surtout parce que d'anciennes applications de rupture.
CGFloat screenScale = [[UIScreen mainScreen] scale];
Cela vous donnera l'échelle de l'écran. Pour tous les iphone et iPodTouches qui n'ont PAS les Écrans de la Rétine sera de retour d'une 1.0 f, tandis que l'Écran Retina de dispositifs de donner un 2.0 f.
Maintenant, si vous voulez obtenir les pixels de la largeur et de la hauteur de l'appareil iOS écran, vous avez juste besoin de faire une chose simple.
CGSize screenSize = CGSizeMake(screenBounds.size.width * screenScale, screenBounds.size.height * screenScale);
En multipliant par l'écran de mise à l'échelle vous procurer les pixels de résolution.
L'utilité de ce code est qu'il va travailler plus tard dans les produits par Apple, comme si l'iPad n'est jamais un Affichage de la Rétine, puis à l'aide de l'échelle sera toujours de vous rendre la vraie résolution en pixels.
Une bonne lecture sur la différence entre les points et les pixels dans iOS peut être lu ici: http://developer.apple.com/library/ios/documentation/2DDrawing/Conceptual/DrawingPrintingiOS/GraphicsDrawingOverview/GraphicsDrawingOverview.html#//apple_ref/doc/uid/TP40010156-CH14-SW7
Utilisez-le dans l'application Délégué: j'utilise le storyboard
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size;
//----------------HERE WE SETUP FOR IPHONE 4/4s/iPod----------------------
if(iOSDeviceScreenSize.height == 480){
UIStoryboard *iPhone35Storyboard = [UIStoryboard storyboardWithName:@"iPhone" bundle:nil];
// Instantiate the initial view controller object from the storyboard
UIViewController *initialViewController = [iPhone35Storyboard instantiateInitialViewController];
// Instantiate a UIWindow object and initialize it with the screen size of the iOS device
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Set the initial view controller to be the root view controller of the window object
self.window.rootViewController = initialViewController;
// Set the window object to be the key window and show it
[self.window makeKeyAndVisible];
iphone=@"4";
NSLog(@"iPhone 4: %f", iOSDeviceScreenSize.height);
}
//----------------HERE WE SETUP FOR IPHONE 5----------------------
if(iOSDeviceScreenSize.height == 568){
// Instantiate a new storyboard object using the storyboard file named Storyboard_iPhone4
UIStoryboard *iPhone4Storyboard = [UIStoryboard storyboardWithName:@"iPhone5" bundle:nil];
// Instantiate the initial view controller object from the storyboard
UIViewController *initialViewController = [iPhone4Storyboard instantiateInitialViewController];
// Instantiate a UIWindow object and initialize it with the screen size of the iOS device
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Set the initial view controller to be the root view controller of the window object
self.window.rootViewController = initialViewController;
// Set the window object to be the key window and show it
[self.window makeKeyAndVisible];
NSLog(@"iPhone 5: %f", iOSDeviceScreenSize.height);
iphone=@"5";
}
} else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
// NSLog(@"wqweqe");
storyboard = [UIStoryboard storyboardWithName:@"iPad" bundle:nil];
}
return YES;
}
Voir la UIScreen référence : http://developer.apple.com/library/ios/#documentation/uikit/reference/UIScreen_Class/Reference/UIScreen.html