programing

iPhone 앱에서 장치의 화면 해상도를 감지하는 방법

bestprogram 2023. 4. 12. 23:07

iPhone 앱에서 장치의 화면 해상도를 감지하는 방법

iPhone 앱에서 앱 실행 중 앱이 실행 중인 장치의 화면 해상도를 감지하는 방법

CGRect screenBounds = [[UIScreen mainScreen] bounds];

그러면 전체 화면의 해상도가 포인트 단위로 표시되므로 아이폰의 경우 보통 320x480이 됩니다.아이폰4가 훨씬 큰 화면 크기를 가지고 있음에도 불구하고 iOS는 640x960 대신 320x480을 돌려준다.이는 대부분 오래된 응용 프로그램이 고장났기 때문입니다.

CGFloat screenScale = [[UIScreen mainScreen] scale];

이렇게 하면 화면의 크기가 표시됩니다.Retina Display를 탑재하지 않은 모든 디바이스는 1.0f를 반환하며, Retina Display 디바이스는 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)

UIScreen 클래스를 사용하면 화면 해상도를 점 및 픽셀 단위로 찾을 수 있습니다.

화면 해상도는 포인트 또는 픽셀 단위로 측정됩니다.화면 크기와 혼동하지 마십시오.화면 사이즈가 작을수록 해상도는 높아집니다.

UIScreen의 사냥개.width' 점의 직사각형 크기를 반환합니다.

UIScreen의 네이티브 Bounds.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

앱 위임에서 사용:스토리보드를 사용하고 있습니다.

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

iOS 8의 경우 이 기능을 사용하면 됩니다.[UIScreen mainScreen].nativeBounds다음과 같은 경우:

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

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

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

이 코드를 사용하면 모든 유형의 장치 화면 해상도를 얻을 수 있습니다.

 [[UIScreen mainScreen] bounds].size.height
 [[UIScreen mainScreen] bounds].size.width

해상도 값 테마가 아닌 모델 해상도 유형을 얻는 것이 목표인 경우 이 Swift 솔루션이 유용할 수 있습니다.

import UIKit

@objc(IphoneModelScreenSize)
public class IphoneModelScreenSize: NSObject {

    // MARK: Enums

    public enum IphoneModelScreenSize: Int {
    case notAnIphone = 0,
         twoThreeOrFour = 1,
         se = 2,
         sixSevenOrEight = 3,
         plus = 4,
         elevenXorXS = 5,
         elevenProMaxOrXsMax = 6
    }

    // MARK: Class properties

    public class func screenSize() -> IphoneModelScreenSize {
        let bounds = UIScreen.main.bounds
        let screenWidth = bounds.size.width
        let screenHeight = bounds.size.height

        switch (screenWidth, screenHeight) {
        case (320.0, 480.0):
          return .twoThreeOrFour
        case (320.0, 568.0):
          return .se
        case (375.0, 667.0):
          return .sixSevenOrEight
        case (414.0, 736.0):
          return .plus
        case (375.0, 812.0):
          return .elevenXorXS
        case (414.0, 896.0):
          return .elevenProMaxOrXsMax
        default:
          return .notAnIphone
        }
    }

    public class func screenSizeStringValue() -> String {
        return screenSizeEnumToString(screenSize())
    }

    // MARK: Private properties

    private class func screenSizeEnumToString(_ screenSize: IphoneModelScreenSize) -> String {
        var screenSizeAsString: String

        switch screenSize {
        case .notAnIphone:
            screenSizeAsString = "Not an Iphone"
        case .twoThreeOrFour:
            screenSizeAsString = "2G, 3G, 3GS, 4 or 4s"
        case .se:
            screenSizeAsString = "5, 5s, 5c or SE"
        case .sixSevenOrEight:
            screenSizeAsString = "6, 6s, 7 or 8"
        case .plus:
            screenSizeAsString = "6+, 6s+, 7+ or 8+"
        case .elevenXorXS:
            screenSizeAsString = "11 Pro, X or Xs"
        case .elevenProMaxOrXsMax:
            screenSizeAsString = "11, Xr, 11 Pro Max or Xs Max"
        }

        return screenSizeAsString
    }

}

언급URL : https://stackoverflow.com/questions/4779221/in-iphone-app-how-to-detect-the-screen-resolution-of-the-device