iPhoneアプリでデバイスの画面解像度を検出する方法


130

iPhoneアプリで、デバイスでアプリを実行している間アプリが実行されているデバイスの画面解像度を検出する方法は?

回答:


349
CGRect screenBounds = [[UIScreen mainScreen] bounds];

これにより、画面全体の解像度がポイント単位で示されるため、iPhoneの場合、通常は320x480になります。iPhone4の画面サイズははるかに大きいですが、iOSでは640x960ではなく320x480が返されます。これは主に、古いアプリケーションが壊れているためです。

CGFloat screenScale = [[UIScreen mainScreen] scale];

これにより、画面のスケールがわかります。Retinaディスプレイを搭載していないすべてのデバイスでは、1.0fが返されますが、Retinaディスプレイデバイスでは2.0f、iPhone 6 Plus(Retina HD)では3.0fが返されます。

iOSデバイス画面のピクセル幅と高さを取得したい場合は、単純なことを1つ実行するだけで済みます。

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

画面のスケールを掛けると、実際のピクセル解像度が得られます。

iOSにおけるポイントとピクセルの違いについては、こちらをご覧ください

編集:(Swiftのバージョン)

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

4
ただし、iPhone 6 Plusの場合、これは「実際のピクセル」の解像度ではありません。これは3倍のスケールで、すべて(OpenGLを除く)コードでレンダリングされる解像度ですが、それは内部で画面のネイティブ解像度1080 x 1920にダウンサンプリングされます。リンクの
RobP

悲しいことに、Appleの「ポイント」と「スケール」の概念は近似にすぎないため、これは画面上の要素の「真の」寸法を提供しません。(iPhone対iPad対iPad miniの仕様を参照してください。)おそらく、存在するさまざまな組み合わせの数を減らすためです。iPhone 6 Plusは特に遠いと思います。
ToolmakerSteve、2015

実際には6+以上離れすぎていません:高さ736 pts / 160(pt / in)= 4.60 "論理的な高さ;実際の画面の高さは4.79"; 5%エラー。iPadはさらに遠くにあります:高さ1024 pts / 160(pt / in)= 6.40 "論理的な高さ;実際の画面の高さは7.76"; 20%エラー。iPad miniは問題ありません。それは元のiPhone密度と一致します。これは、ほとんどの目的で、iPadソフトウェアをiPad miniでテストして(使用可能であることを確認するため)、ほとんどのiPadが画像を20%拡大する(iPhoneまたはiPad miniと比較して)ことを単に無視することを意味します。
ToolmakerSteve、2015

1
@RobPでは、iPhone 6 Plusでこれをどのように解決しますか?
Crashalot

1
@Crashalotは「これを解決する」という意味がわかりませんか?これは、画面の解像度を取得するときの目的に依存します。プログラマに関する限り、Jman012の答えは正しいので、1242x2208または2208x1242スペースにレンダリングします。いや、それは私たちが提供する起動画像の解像度です。次に、ハードウェアがこの画像をダウンサンプリングして、ピクセル数の少ない物理画面に表示するという事実は、コードが認識していない「実装の詳細」になります。
RobP 2016

21

UIScreenクラスを使用すると、ポイントとピクセルで画面解像度を見つけることができます。

画面解像度はポイントまたはピクセルで測定されます。画面サイズと混同しないでください。画面サイズが小さいほど、解像度が高くなります。

UIScreenの「bounds.width」は長方形のサイズをポイントで返します ここに画像の説明を入力してください

UIScreenの 'nativeBounds.width'はピクセル単位の長方形のサイズを返します。この値はPPI(ポイント/インチ)として検出されます。デバイス上の画像の鮮明さと明瞭さを示します。 ここに画像の説明を入力してください

UIScreenクラスを使用して、これらすべての値を検出できます。

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)")

コンソール

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

8

App Delegateで使用:ストーリーボードを使用しています

- (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;
 }

5

iOS 8 [UIScreen mainScreen].nativeBoundsでは、次のようにこれを使用できます。

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

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

4

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");
}

NSLog(@ "%d"、[[UIScreen mainScreen] scale]);に入れている場合の返信に感謝します。0とNSLog(@ "%@"、[[UIScreen mainScreen] scale]);を返します。それはnilを与えますPlsは、画面解像度を取得する方法、またはシミュレータで実行中に正しい解像度を与えるかどうかをテストする方法を教えてくれます
ios

2
試すNSLog(@"%f",[[UIScreen mainScreen] scale]);
vikingosegundo

0

このコードを使用すると、あらゆる種類のデバイスの画面解像度を取得するのに役立ちます

 [[UIScreen mainScreen] bounds].size.height
 [[UIScreen mainScreen] bounds].size.width
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.