iPhone - getting the size of a view in pixels - iphone

I have a camera taken picture, and I'd like to resize it to the exact size of a view. But... bounds.size on an iPhone 4 does not take account of the retina display.
I'd like a code that can give me the real pixel size of the view, so it can work on any device without having to test / to know the kind of hardware it runs on.
Do you know how I may do this ?

Every view has the contentScaleFactor property, which is typically 1.0 (3G, 3GS, iPad) or 2.0 (retina screens). Another approach is to use [[UIScreen mainScreen] scale], which has the same values.
note to remain compatible with iOS versions prior to 4.0, you have to check if the method is available using respondsToSelector:#selector(contentScaleFactor) before getting the value.

Related

iPhone and iPad screen resolution

I m developing a universal app.I want to know will the screen resolutions (320 * 480) for iphone and (768 *1024 )in iPad will work for all iphone's (iPhone 3g,iPhone4 etc) and all iPads.Because based on these screen resolutions I m setting textField's, UILabel's width in both iPhone and iPad.Will these work for retinas and nonretinas ?
Retina iPhones and iPads use the same coordinate system as non-Retina devices. Presently all iPads have a logical coordinate space of 768x1024, and all iPhones except the iPhone 5 have a logical coordinate space of 320x480. Your code should work fine on both Retina and non-Retina devices.
On an iPhone 5, your app will be shown with black bars at the top of the screen unless you tell iOS that you want to use the full screen by including a Default.png for the extended screen resolution.
You can check the screen resolution with [[UIScreen mainScreen] bounds]. This value will be the same on Retina and non-Retina devices. You can detect a Retina device by checking the value of [[UIScreen mainScreen] scale]; the value here is the number of physical pixels per unit of logical coordinate space (1.0 for non-Retina, 2.0 for Retina).
UIKit and CoreGraphics work with points rather than pixels.
Both the retina and non-retina devices have the same number of points, but a different amount of pixels. This means that the same point values can mean a different pixel value on different devices.
To answer your question, yes the same layout UILabel widths will display the same on retina and non retina devices.
From the Apple Developer Documentation :
In iOS, all coordinate values and distances are specified using floating-point values in units referred to as points. The measurable size of a point varies from device to device and is largely irrelevant. The main thing to understand about points is that they provide a fixed frame of reference for drawing.
Have a look at the Points vs. Pixels section in the View Programming Guide:
http://developer.apple.com/library/ios/documentation/windowsviews/conceptual/viewpg_iphoneos/WindowsandViews/WindowsandViews.html#//apple_ref/doc/uid/TP40009503-CH2-SW15
You can always use the capabilities to get the OS and do what you need to your interface.
var pattern:RegExp = /iphone5/i;
var str:String = Capabilities.os;
if (str.search(pattern)==0)
{
trace('iPhone5 Detected. Alter height.');
}else{
trace('No need to change dimensions if developing at 960x640');
}

How do I use different images, depending on iOS device type?

I want to use different images in my iPhone application, depending on whether the current device is an iPhone 3G, iPhone 4, or iPad. I've tried getting the screen resolution (size) of the device, but it always returns 320 X 480 for the different iPhones.
How do I determine which type of device I'm running on so that I can provide the correct images?
I do not want to get the iPhone OS version.
There's most likely no need to do what you're attempting to do, as iOS has built in support for loading images at the appropriate size for the iPhone (retina and pre-retina resolutions) and iPad.
Read the Resource Programming Guide (specifically the "Specifying High-Resolution Images in iOS" and "iOS Supports Device-Specific Resources" section) and the iOS Application Programming Guide for more information.
Incidentally, both the pre and post retina iPhone/Pods have a point resolution of 320 X 480. Apple distinguishes between point sizes (resolution independent) and pixel size to help things along the way.
If you want this info for the purposes of selecting the proper graphics for your resolution, then middaparka's answer is what you're looking for.
However, if you happen to need device model or resolution info for a different purpose, then UIDevice and UIScreen may be what you want.
NSString *myDeviceModel = [[UIDevice currentDevice] model];
CGRect myScreenSize = [[UIScreen mainScreen] bounds].size;
i think below one solve my problem
https://github.com/erica/uidevice-extension/blob/master/UIDevice-Hardware.m
Thanks

Advice for a universal OpenGL ES application (iPhone & iPad)?

I am about to convert my iPhone OpenGL ES based app into a universal (iPhone + iPad) app, using iOS 4.2. What advice would you give me? Are there any notable pitfalls in doing so?
Make sure you get the bounds rectangle for the screen dimensions rather than hardcoding the view size or anything based on window coordinates.
[[UIScreen mainScreen] bounds];
Recognize that if your application wasn't fill-limited (i.e. the hardware can only fetch so many texels and display so many fragments per second) on the iPhone 3GS or earlier (640x480) screen. That it very well may be fill limited on iPad or iPhone 4 (1024x768 or 960x640) screen size respectively.

iphone 4 Simulator reporting 480x320 as the screen size?

I am debugging an application using iPhone 4 Simulator. I have selected it from the simulator menu (device = iPhone 4). When I run the app, the screen size is reported as 480x320 !!??
Is there something I have to modify on this app of mine (originally built for 3G/3gs) in order to make it run on iPhone 4 (yes, I have recreated all artwork as 960x640 and the artwork is on the bundle, but it is scaling it down to half the size... because it is running on 3G/3GS mode, instead of hi res).
running
CGRect cgRect =[[UIScreen mainScreen] bounds];
will result in (0,0,480,320).
Any clues?
thanks for any help.
Recommended read: http://developer.apple.com/iphone/library/documentation/iphone/conceptual/iphoneosprogrammingguide/SupportingResolutionIndependence/SupportingResolutionIndependence.html
One point does not necessarily
correspond to one pixel on the screen.
A sample project, download here.
The iPhone is making the move into resolution independence. This means that things aren't measured in pixels, but in points. Points don't always correspond to pixels.
The screen size of the iPhone 3GS (and previous) is 480x320, in both points an pixels. They correspond on these devices, but in newer devices (like iPhone 4) they do not.
The iPhone 4's screen size is 960x640 in pixels, but its logical screensize is still 480x320 in points.
This allows you to keep your frame, point and size values in their original values and still support larger resolution devices.
On iPhone 4 you need to have #2x somewhere in the name of the image file and it will be used automatically on the higher resolution devices.
When #2x images are loaded, they are loaded as their original resolution, but their size property is halved to be able to work with the logical point measurments.
For instance, an image with an original size of 960x640 will report its size as 480x320 when it's asked for.
instead of bounds use currentMode
CGSize pixelBufferSize = [[[UIScreen mainScreen] currentMode] size];
you will get actual resolution

Is it possible to detect DPI in iPhone OS, or Android?

I'm doing some research on making a game that will be able to scale its graphical resources to suit the DPI of whatever device it's on.
In order to do this, I would like to be able to query the DPI of the device, to appropriately scale the assets.
It's a 2D game, and the art style suits arbitrary scaling quite well.
example of what I would do with res/dpi combos:
iPhone/iTouch, at 320x480 (163 dpi) - text will be the normal size
iPhone4, at 640x960 (326 dpi) - text will be twice as large
iPad, at 768x1024 (132 dpi) - text will probably be capped at some minimum size, to take advantage of the greater screen real estate.
So, on iPhone OS, is there a way to query the screen's DPI?
(and as a side note - is this possible on Android devices at all?)
You can fetch this information in Android with the DisplayMetrics class.
I'm not sure if there's a direct way to get the DPI, as you generally don't need to know it. The UIScreen class does expose a scale factor. For iPhone <= 3GS and iPad it should be 1.0, for iPhone 4 it should be 2.0.
Sight compiled:
UIScreen *theScreen = [UIScreen mainScreen];
float scaleFactor = 1.0f;
if ([theScreen respondsToSelector:#selector(getScale)]) {
scaleFactor = theScreen.scale;
}
// use scaleFactor to determine size of fonts/whatever
But, the way the new OS is implemented in regards to differing screen resolutions means you usually won't have to custom scale depending on DPI. Things like images will either be scaled up or it will automatically choose a specially named higher resolution image, and drawing operations are also handled automatically (like drawing a 1point line now draws a 2px line on the Iphone 4). Fonts may be handled similarly, I don't think I came across anything font specific. Read up on the new docs for info:
http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/SupportingResolutionIndependence/SupportingResolutionIndependence.html#//apple_ref/doc/uid/TP40007072-CH10