web-dev-qa-db-fra.com

dans l'iPhone App Comment détecter la résolution d'écran de l'appareil

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 est en cours d'exécution?

127
ios
CGRect screenBounds = [[UIScreen mainScreen] bounds];

Cela vous donnera la résolution de l’ensemble de l’écran en points, il s’agira donc généralement de 320x480 pour les iPhones. Même si l'iPhone4 a une taille d'écran beaucoup plus grande, iOS restitue encore 320x480 au lieu de 640x960. Ceci est principalement dû aux anciennes applications cassées.

CGFloat screenScale = [[UIScreen mainScreen] scale];

Cela vous donnera l'échelle de l'écran. Pour tous les appareils ne disposant pas d’affichage Retina, cela renvoie un 1.0f, tandis que les appareils Retina Display donnent un 2,0f et que l’iPhone 6 Plus (Retina HD) émet un 3.0f.

Maintenant, si vous voulez obtenir la largeur et la hauteur en pixels de l'écran du périphérique iOS, il vous suffit de faire une chose simple.

CGSize screenSize = CGSizeMake(screenBounds.size.width * screenScale, screenBounds.size.height * screenScale);

En multipliant par l'échelle de l'écran, vous obtenez la résolution réelle en pixels.

Une bonne lecture sur la différence entre les points et les pixels dans iOS peut être lue ici .

EDIT: (Version pour Swift)

let screenBounds = UIScreen.main.bounds
let screenScale = UIScreen.main.scale
let screenSize = CGSize(width: screenBounds.size.width * screenScale, height: screenBounds.size.height * screenScale)
343
James Linnell

La classe UIScreen vous permet de trouver la résolution d’écran en points et en pixels.

Les résolutions d'écran sont mesurées en points ou en pixels. Il ne faut jamais confondre avec la taille de l'écran. Une taille d'écran plus petite peut avoir une résolution plus élevée.

'Bounds.width' de UIScreen retourne une taille rectangulaire en points enter image description here

'NativeBounds.width' de UIScreen renvoie une taille rectangulaire en pixels. Cette valeur est détectée comme étant PPI (point par pouce). Affiche la netteté et la netteté de l'image sur un périphérique. enter image description here

Vous pouvez utiliser la classe UIScreen pour détecter toutes ces valeurs.

Swift3

// Normal Screen Bounds - Detect Screen size in Points.
let width = UIScreen.main.bounds.width
let height = UIScreen.main.bounds.height
print("\n width:\(width) \n height:\(height)")

// Native Bounds - Detect Screen size in Pixels.
let nWidth = UIScreen.main.nativeBounds.width
let nHeight = UIScreen.main.nativeBounds.height
print("\n Native Width:\(nWidth) \n Native Height:\(nHeight)")

Console

width:736.0 
height:414.0

Native Width:1080.0 
Native Height:1920.0

Swift 2.x

//Normal Bounds - Detect Screen size in Points.
    let width  = UIScreen.mainScreen.bounds.width
    let height = UIScreen.mainScreen.bounds.height

// Native Bounds - Detect Screen size in Pixels.
    let nWidth  = UIScreen.mainScreen.nativeBounds.width
    let nHeight = UIScreen.mainScreen.nativeBounds.height

ObjectiveC

// Normal Bounds - Detect Screen size in Points.
CGFloat *width  = [UIScreen mainScreen].bounds.size.width;
CGFloat *height = [UIScreen mainScreen].bounds.size.height;

// Native Bounds - Detect Screen size in Pixels.
CGFloat *width  = [UIScreen mainScreen].nativeBounds.size.width
CGFloat *height = [UIScreen mainScreen].nativeBounds.size.width
19
Taimur Ajmal

Utilisez-le dans App Delegate: 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;
 }
8
ios_av

Pour iOS 8, nous pouvons simplement utiliser ce [UIScreen mainScreen].nativeBounds , comme ça:

- (NSInteger)resolutionX
{
    return CGRectGetWidth([UIScreen mainScreen].nativeBounds);
}

- (NSInteger)resolutionY
{
    return CGRectGetHeight([UIScreen mainScreen].nativeBounds);
}
5
boog

Voir la référence UIScreen: http://developer.Apple.com/library/ios/#documentation/uikit/reference/UIScreen_Class/Reference/UIScreen.html

if([[UIScreen mainScreen] respondsToSelector:NSSelectorFromString(@"scale")])
{
    if ([[UIScreen mainScreen] scale] < 1.1)
        NSLog(@"Standard Resolution Device");

    if ([[UIScreen mainScreen] scale] > 1.9)
        NSLog(@"High Resolution Device");
}
4
Constantin

Utilisez ce code, il vous aidera à obtenir la résolution d'écran de tout type d'appareil

 [[UIScreen mainScreen] bounds].size.height
 [[UIScreen mainScreen] bounds].size.width
0
Shahzaib Maqbool