在iPhone應用程序中, 在設備上運行應用程序時如何檢測運行App的設備的屏幕分辨率?在iPhone應用程序中如何檢測設備的屏幕分辨率
122
A
回答
328
CGRect screenBounds = [[UIScreen mainScreen] bounds];
這會給你整個屏幕的分辨率分,所以這將最典型的爲320x480像素的iPhone。儘管iPhone4擁有更大的屏幕尺寸,但iOS仍然支持320x480而不是640x960。這主要是因爲較舊的應用程序中斷。
CGFloat screenScale = [[UIScreen mainScreen] scale];
這會給你屏幕的比例。對於沒有視網膜顯示器的所有設備,這將返回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中點和像素之間的區別,可以閱讀here。
編輯:(版本爲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
見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");
}
8
使用它在應用代表:我用故事板
- (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];
[email protected]"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);
[email protected]"5";
}
} else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
// NSLog(@"wqweqe");
storyboard = [UIStoryboard storyboardWithName:@"iPad" bundle:nil];
}
return YES;
}
4
對於iOS 8,我們可以只使用這個[UIScreen mainScreen].nativeBounds
,這樣的:
- (NSInteger)resolutionX
{
return CGRectGetWidth([UIScreen mainScreen].nativeBounds);
}
- (NSInteger)resolutionY
{
return CGRectGetHeight([UIScreen mainScreen].nativeBounds);
}
9
UIScreen類讓你找到屏幕點和像素的解決方案。
屏幕分辨率是以點或像素來衡量的。它不應該與屏幕大小混淆。較小的屏幕尺寸可以具有較高的分辨率。
UIScreen的「nativeBounds.width」返回的矩形尺寸在Pixels.This值「bounds.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
夫特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
0
使用此代碼,這將有助於獲得設備的屏幕分辨率的任何類型的
[[UIScreen mainScreen] bounds].size.height
[[UIScreen mainScreen] bounds].size.width
相關問題
- 1. 如何檢測移動設備的屏幕分辨率?
- 2. Windows 8應用程序中的屏幕分辨率檢測
- 3. 屏幕分辨率檢測
- 4. 檢測屏幕分辨率
- 5. 如何遷移iPhone 5屏幕分辨率的應用程序?
- 6. iPhone模擬器屏幕分辨率vs iPhone屏幕分辨率
- 7. 如何從C#winform應用程序檢索屏幕分辨率?
- 8. 如何根據設備/屏幕分辨率調整屏幕?
- 9. Android設備屏幕分辨率
- 10. Corona SDK跨設備屏幕分辨率
- 11. 檢測客戶端屏幕分辨率
- 12. 在iphone中獲取屏幕分辨率
- 13. 屏幕分辨率測試
- 14. 如何在javascript中檢測屏幕分辨率
- 15. 如何檢測Delphi中的屏幕分辨率變化?
- 16. 適應屏幕分辨率
- 17. 如何在Windows Phone設備上獲取屏幕分辨率
- 18. 在.Net中設置屏幕分辨率
- 19. 檢測低分辨率android設備
- 20. 如何確定設備上的高分辨率屏幕
- 21. 如何獲得設備(Windows Phone)的屏幕分辨率
- 22. 如何獲得Android設備的屏幕分辨率..?
- 23. 設備(iPhone 5)使用Worklight 6.0.0.1時未檢測到分辨率
- 24. iPhone和iPad的屏幕分辨率
- 25. 適合Web的Iphone屏幕分辨率
- 26. 屏幕分辨率
- 27. 屏幕分辨率
- 28. 屏幕分辨率
- 29. 屏幕分辨率
- 30. 屏幕分辨率
thanx的迴應,如果我在的NSLog把它(@ 「%d」,[UIScreen mainScreen] scale]);它給出0 ......和NSLog(@「%@」,[[UIScreen mainScreen] scale]);它給nil請讓我知道如何獲得屏幕分辨率或如何測試在模擬器上運行它時是否給出正確的分辨率 – ios 2011-01-24 07:22:45
嘗試`NSLog(@「%f」,[[UIScreen mainScreen] scale]);` – vikingosegundo 2011-01-24 12:12:50
@ vikingosegundo,謝謝 – ios 2011-01-25 03:34:02