I can't take good results when I try to read resolution of my device.
Such code
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
NSString* name = [self machineName];
gives
1024*768 and iPad4,5
machineName code:
#import <sys/utsname.h>
-(NSString*) machineName()
{
struct utsname systemInfo;
uname(&systemInfo);
return [NSString stringWithCString:systemInfo.machine
encoding:NSUTF8StringEncoding];
}
What am I doing wrong? I can't understand. It must be 2048*1536
Bound returns the screen dimensions in points.
You need to query CGFloat scale = [[UIScreen mainScreen] scale];
and multiply this by the width/height to get the pixel value
Try this one to get screen resolution in points...
(1) Find the screen scale using below method
CGFloat screenScale = [[UIScreen mainScreen] scale];
This will give you the scale of the screen. For all iPhones and iPodTouches that do NOT have Retina Displays will return a 1.0f, while Retina Display devices will give a 2.0f.
Now if you want to get the pixel width & height of the iOS device screen you just need to do one simple thing.
(2) By multiplying by the screen's scale you get the actual pixel resolution.
CGSize screenSize = CGSizeMake([[UIScreen mainScreen] bounds].size.width * screenScale, [[UIScreen mainScreen] bounds].size.height * screenScale);
For more details read this link
Related
I run my app on my iPhone 7 + device and when I try to log the Scale and screen bound sizes, What I get is related to iPhone 5/5s/5c/SE, NOT to my iphone 7 plus specification.
This is what I get in log:
CGFloaf scale = [[UIScreen mainScreen] scale];
CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;
NSLog (#"Scale: %f", scale);
NSLog (#"screenHeight: %f", screenHeight);
Output :
Scale: 2.000000
screenHeight: 568.000000
Why is that??! I appreciate any help.
I have a UIView class I'm integrating into a project. I plan to add MySubView to MyViewController. There's a constant class where the screen width is hard-coded as follows:
MySubviewConstants.h
#define SCREEN_WIDTH 300.0f
I'd like to define this constant as the width of the subview of the device it's running on, not a hard-coded number. I think this should be an easy 25 pointer.
This code will get the actual device's screen width:
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
Or perhaps using a variant of:
[[[[UIApplication sharedApplication] keyWindow] rootViewController].view convertRect:[[UIScreen mainScreen] bounds] fromView:nil].size
I am using cocos2d in iOS .
I am having a problem detecting the iphone5 resolution(1136*640).
I am using code:
CGRect screenBounds = [[UIScreen mainScreen] bounds];
When we run app on iPhone simulator (4 inch), it gives screenWidth and screnHeight-320,480 respectively.
Can anyone tell me the solution for this?
Write this code in .m file.
#define WIDESCREEN ( fabs( ( double )[ [UIScreen mainScreen ]bounds ].size.height-( double )568 ) < DBL_EPSILON )
Now you can know the screen size like this.
if(WIDESCREEN)
{
NSLog(#"iPhone5 is here");
}
else
{
NSLog(#"iPhone4 is here");
}
you can add checks in your code for iPhone 4inch screens and 3.5 inch screens
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 480)
{
// iPhone Classic
// set your frames for classic iPhone here
}
if(result.height == 568)
{
// iPhone 5, iPhone 5S, iPhone 5C
// set your frames for 4 inch iPhone here
}
iPhone 5 resolution is 1136*640 only. If you wanna check just Log below Code
the resolution it will give exactly (1136*640)
CGRect screenBounds = [[UIScreen mainScreen] bounds];
CGFloat screenScale = [[UIScreen mainScreen] scale];
CGSize screenSize = CGSizeMake(screenBounds.size.width * screenScale, screenBounds.size.height * screenScale);
NSLog(#"Screen SIze %#",NSStringFromCGSize (screenSize));
#define IS_IPHONE5 ([[UIScreen mainScreen] bounds].size.width >= 568 || [[UIScreen mainScreen] bounds].size.height >= 568)?YES:NO
#define IS_IPHONE4 ([[UIScreen mainScreen] bounds].size.width >= 480 || [[UIScreen mainScreen] bounds].size.height >= 480)?YES:NO
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
if(IS_IPHONE4)
{
// iPhone 4
}
if(IS_IPHONE5)
{
// iPhone 5
}
}
I have used this method for detecting the width and height of screen. But its showing the width as 768 and height as 1024 in portrait and landscape also.
CGRect screenBounds = [[UIScreen mainScreen] bounds];
float widthfloat= screenBounds.size.width;
float heightfloat= screenBounds.size.height;
NSLog(#" width float %f",widthfloat);
NSLog(#"height float %f",heightfloat);
NSLog(#"width view %f \n height view %f", self.view.bounds.size.width, self.view.bounds.size.height);
NSLog(#"width %f \n height %f", screenBounds.size.width, screenBounds.size.height);
float wvalue = [[UIScreen mainScreen] bounds].size.width;
float hvalue = [[UIScreen mainScreen] bounds].size.height;
NSLog(#" wvalue %f",wvalue);
NSLog(#"hvalue %f",hvalue);
CGFloat width1 = [UIScreen mainScreen].bounds.size.width;
CGFloat height1 = [UIScreen mainScreen].bounds.size.height;
NSLog(#"width1 %f",width1);
NSLog(#"height1 %f",height1);
CGFloat screenScale = [[UIScreen mainScreen] scale];
CGRect screenBounds1 = [[UIScreen mainScreen] bounds];
CGSize screenSize = CGSizeMake(screenBounds1.size.width * screenScale, screenBounds1.size.height * screenScale); if (screenSize.height==1136.000000)
NSLog(#"abcd1 %f",screenBounds1.size.width);
NSLog(#"abcd2 %f",screenBounds1.size.height);
NSLog(#"efgh1 %f",screenSize.width);
NSLog(#"efgh2 %f",screenSize.height);
bounds contains the bounding rectangle of the screen, measured in points. It does not care about screen orientation.
You can calculate the screen size, I do something like this for me -
-(CGSize)screenSize
{
CGSize size = [UIScreen mainScreen].applicationFrame.size;
if(UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation))
return size;
else
{
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
if (size.height > 480.0)
// This is iPhone-5
size = CGSizeMake(568.0, 320.0);
else
// This is iPhone-4
size = CGSizeMake(480.0, 320.0);
}
else
{
// This is iPad
size = CGSizeMake(1024.0, 768.0);
}
if (![[UIApplication sharedApplication] isStatusBarHidden])
size.height -= 20.0;
return size;
}
}
Note: The method will give you the height after reducing the height of the statusbar if available.
In iPhone App,
while running the App on device How to detect the screen resolution of the device on which App is running?
CGRect screenBounds = [[UIScreen mainScreen] bounds];
That will give you the entire screen's resolution in points, so it would most typically be 320x480 for iPhones. Even though the iPhone4 has a much larger screen size iOS still gives back 320x480 instead of 640x960. This is mostly because of older applications breaking.
CGFloat screenScale = [[UIScreen mainScreen] scale];
This will give you the scale of the screen. For all devices that do not have Retina Displays this will return a 1.0f, while Retina Display devices will give a 2.0f and the iPhone 6 Plus (Retina HD) will give a 3.0f.
Now if you want to get the pixel width & height of the iOS device screen you just need to do one simple thing.
CGSize screenSize = CGSizeMake(screenBounds.size.width * screenScale, screenBounds.size.height * screenScale);
By multiplying by the screen's scale you get the actual pixel resolution.
A good read on the difference between points and pixels in iOS can be read here.
EDIT: (Version for 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 class lets you find screen resolution in Points and Pixels.
Screen resolutions is measured in Points or Pixels. It should never be confused with screen size. A smaller screen size can have higher resolution.
UIScreen's 'bounds.width' return rectangular size in Points
UIScreen's 'nativeBounds.width' return rectangular size in Pixels.This value is detected as PPI ( Point per inch ). Shows the sharpness & clarity of the Image on a device.
You can use UIScreen class to detect all these values.
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
Use it in App Delegate: I am using 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;
}
For iOS 8 we can just use this [UIScreen mainScreen].nativeBounds , like that:
- (NSInteger)resolutionX
{
return CGRectGetWidth([UIScreen mainScreen].nativeBounds);
}
- (NSInteger)resolutionY
{
return CGRectGetHeight([UIScreen mainScreen].nativeBounds);
}
See the UIScreen Reference: 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");
}
Use this code it will help for getting any type of device's screen resolution
[[UIScreen mainScreen] bounds].size.height
[[UIScreen mainScreen] bounds].size.width
If your goal is to get the model resolution type and not the resolution values themeselfs, this Swift solution might be helpful:
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
}
}