iPhone App에서 기기의 화면 해상도를 감지하는 방법


130

iPhone App에서 기기에서 앱을 실행하는 동안 앱이 실행되는 기기의 화면 해상도를 감지하는 방법은 무엇입니까?

답변:


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 기기 화면의 픽셀 너비 및 높이를 얻으려면 간단한 작업을 수행하면됩니다.

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

안타깝게도 애플의 "포인트"와 "스케일"개념은 근사치에 불과하기 때문에 화면에있는 요소의 "실제"치수를 제공하지 않습니다. (iPhone vs iPad vs iPad mini 사양을 참조하십시오.) 존재할 수있는 다른 조합의 수를 줄일 수 있습니다. iPhone 6 Plus가 특히 멀리 떨어져 있다고 생각합니다.
ToolmakerSteve

실제로 6+는 그리 멀지 않습니다 : 높이 736 pts / 160 (pt / in) = 4.60 "논리 높이; 실제 화면 높이는 4.79"; 5 % 오류. iPad가 훨씬 더 먼 경우 : 높이 1024pts / 160 (pt / in) = 6.40 "논리 높이; 실제 화면 높이는 7.76"; 20 % 오류 iPad mini 는 괜찮습니다. 원래 iPhone 밀도와 일치합니다. 대부분의 경우 iPad mini 에서 iPad 소프트웨어를 테스트 한 다음 (사용 가능한지 확인해야 함) 대부분의 iPad가 이미지를 20 % 확대 한 사실 (iPhone 또는 iPad mini와 비교)을 무시하면됩니다.
ToolmakerSteve

1
@RobP 그렇다면 iPhone 6 Plus에서 어떻게 해결할 수 있습니까?
Crashalot

1
@Crashalot '이 문제를 해결'한다는 것이 무엇을 의미하는지 잘 모르시겠습니까? 화면 해상도를 얻을 때 염두에 두어야 할 목적에 따라 다릅니다. 프로그래머와 관련하여 Jman012의 답변은 정확하며 1242x2208 또는 2208x1242 공간으로 렌더링합니다. 그것은 심지어 우리가 발사 이미지를 제공하는 해상도이기도합니다. 하드웨어가이 이미지를 다운 샘플링하여 더 작은 픽셀 수로 물리적 화면에 표시한다는 사실은 코드가 인식하지 못하는 '구현 세부 사항'이 될 것입니다.
RobP

21

UIScreen 클래스를 사용하면 포인트 및 픽셀에서 화면 해상도를 찾을 수 있습니다.

화면 해상도는 포인트 또는 픽셀로 측정됩니다. 화면 크기와 혼동해서는 안됩니다. 화면 크기가 작을수록 해상도가 높아질 수 있습니다.

UIScreen의 'bounds.width'는 사각형 크기를 포인트로 반환합니다. 여기에 이미지 설명을 입력하십시오

UIScreen의 'nativeBounds.width'는 픽셀 단위의 사각형 크기를 반환합니다.이 값은 PPI (Point per inch)로 감지됩니다. 장치에서 이미지의 선명도와 선명도를 보여줍니다. 여기에 이미지 설명을 입력하십시오

UIScreen 클래스를 사용하여 이러한 모든 값을 감지 할 수 있습니다.

스위프트 3

// 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

스위프트 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

목표 C

// 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.