Swift image size based on device displayed on - swift

I have a view controller I made. There is a lot of information displayed on it, including labels and images. I want to set it so if the device used is the iPhone 4 then an image does not display, to help conserve the view controller real estate. How do I do this in Swift?

For detecting devices (like iPhone 4):
var device = UIDevice.currentDevice().model
This string would show the device model. For instance, iPhone3,1 represents that it is an iPhone 4.
You can find the string for each device (including iPod and iPad) in this page: UIDevice currentDevice model possible values
if device == "iPhone3,1" {
imageview.hidden = true
}
The above method only works on real devices, but not simulators.
If you only want to hide the UIImageView on certain screen sizes regardless of the device type, you can use the method below.
if UIScreen.mainScreen().nativeBounds.height == 960.0 {
// code for iPhone 4 or 4S
} else {
// code for the rest
}
Checking the width like what Leonardo Savio Dabus answered would not work, since iPhone 5 has the same width as iPhone 4, but iPhone 5 has a larger screen than iPhone 4.

You can use this code to detect the device during runtime:
var device = UIDevice.currentDevice().model

Related

Universal app with .#2x only

My universal app has two storyBoards. One for iPhone and one for iPad. A lot of the code has been coded programatically and both storyBoards share the same ViewControllers. An example of how this works in view controller as follows:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){ iPadInUse = YES;}
else { iPadInUse = NO;}
So, if I have a image that needs to be displayed on both devices - I write the following:
if (iPadInUse == YES) { recordViewImage = #"iPadRecordPanel#2x.png"
// x and y positions to adjusted accordingly }
else { recordViewImage = #"recordingPanel#2x.png"
// // x and y positions to adjusted accordingly}
On the simulators everything works fine for all devices (retina /non retina). The only actual device I have is an iPhone 4S (all works fine). Is this ok what I'm doing as do not actually have any non retina images loaded fro non retina devices (all images suffix with #2x.png ??
Will my app get rejected because of this ??
No, this is not ok.
On devices that do not have retinta screen more memory will be used to load the retina images.
You should always provide non retina and retina images and never load #2x images directly. iOS will load the correct image for you.
Your app will not get rejected, and everything will work fine.
But there are two issues:
On non-retina devices, App will load larger images. Which is not required there..so memory utilization will increase there without any outcome.
You don't need to write conditional code for iphone and ipad for setting images.
You can use a tilde while naming the image files:
iPhone:
image#2x~iphone.png (retina only)
image#2x.png (retina only)
image~iphone.png
image.png
iPad:
image#2x~ipad.png (retina only)
image#2x.png (retina only)
image~ipad.png
image.png
It will not display proper image when you will give it name like iPadRecordPanel#2x.png. You can give name iPadRecordPanel.png and it will take 2x image of it from bundle. You can not give suffix with #2x. it will automatically identify proper image and display it.
No your app will not be rejected but you should not do this because not providing non-retina images may decrease performance and image quality on older devices due to the required down-scale operations.
Hope it helps you.

AS3 detect ipad or iphone in Flash Professional CS6

Using Flash Professional CS6, I've created a game that looks and plays great on an iPad 2 and 3. So now I'm ready to make the app "Universal" by making one that works better for the smaller screen on iPhones.
What I'd like to do, is detect in my first frame something like this:
if iPhone {
gotoAndPlay(2, "Scene 1");
}
else if iPad {
gotoAndPlay(3, "Scene 2");
}
else {
trace("Not an iOS Device");
}
If seen posts from 2010 where people retrieved the height and width of the device in px, but surely there's a better way right?
Capabilities class has useful info. The os property should help determine the device but you'll need to experiment to see what each device returns.
However, i would suggest basing logic on screen size rather than device type - that way its more flexible.

How to simply switch from iphone 4 size to iphone 5 size?

I would like to know how could I make my application enable for iphone 5 standards (especially screen-size).
I currently have an application designed for iPhone 4 so all the views are 460 px height.
Can I do some simple and automamic update to make it for iPhone 5? Without ruining the iPhone 4 style, I would like this app to be avaible on both.
There is not magic code that will make app adjust it self, just the Default-568h#2x.png top tell iOS that you app support the new 4" height.
The most important part is how you have set the Autoresize mask of you views. If then are set to grow (UIViewAutoresizingFlexibleHeight) then they will grow to fill the screen.
Check every view that the alginment of button have the correct Autoresize mask, for example if a button is ment to be at the button of the screen make sure that the Autoresize mask is set to the fix it self to the bottom and nog the default top.
First, see what your app looks like on iPhone 5. This can be done by including a Default-568h#2x.png within your project. This is the launch image for iPhone 5, and indicates to the OS that you support the taller screen size.
If you've programmmed your application well (for example, with decent Autoresize masks), the OS should take care of most of it for you, and it'll look decent already. If you want to use AutoLayout in iOS 6, this will also help for the iPhone 5.
If it does not look that great, then you can do some conditional modifications for iPhone 5 using the macro:
#define IsTallPhone() ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height == 568)
So for example:
if (IsTallPhone()) {
frame = CGRectMake(0,0,200,500); // Use Tall Frame
}
else {
frame = CGRectMake(0,0,200,300); // Use Normal Frame
}
The conditional can be done for anything, for example to load a different .xib file. It entirely up to you.
Open your project in xcode 4.5 and just add a default image of iPhone5 resolution in launch images. all other screens will be stretched to fill the iPhone 5 screen. go to target summary and add retina 4 inch launch image.
But it really depends on the graphics they may appear stretched go for separate xib if required

iPhone: Image on iOS 6 is showing blurred?

I have an image named abc.png of size 320 by 156 which runs well on iPhone 4 and an image named as abc#2x.png of size 640 by 312 which also works fine. but when Please enlighten me on how to use the image for the iPhone 6 device?
First of all there is nothing like iPhone6 as of now, it is either iOS6 or iPhone 5.
But I guess you are talking about iPhone 5 with screen resolution 640X1136.
There is no in-built way to select image for iPhone 5.
if you have abc.png and abc#2x.png in resources , and you use a code like:
UIImage * image = [UIImage imageNamed:#"abc.png"];
it will pick abc.png for non-retina display and abc#2x.png for retina display(that includes iPhone 4S and iPhone 5).
If you want to load separate image for iPhone 5, you will need to check the screen height:
if(screenHeight == 480)
{
//iphone 3,3G,4
}
else if(screenHeight == 960)
{
//iphone 4S
}
else if(screenHeight == 1136)
{
//iphone 5
}
This way you may load different images for different iphones.
Please uprate and check the answer if it answers ur question.
The answer to your question, I'm afraid to say, is something called AutoLayout. That's the solution Apple came up with to deal with the different form factor for iOS 6 views. Only #2x images are available, but with AutoLayout, you can tell it exactly how to behave.
Here's a nice tutorial on how to use AutoLayout, I tried it and it works well:
http://www.raywenderlich.com/20881/beginning-auto-layout-part-1-of-2
http://www.raywenderlich.com/20897/beginning-auto-layout-part-2-of-2

Is there an image name convention for iPhone 5

I'm using an image that will fill the full view of an iPhone. For iPhone 3GS we name it image.png for iPhone 4 we name it image#2x.png -is there an image name convention for iPhone 5 where it will automatically substitute it, or do I need to manually check which device I'm on?
I've read elsewhere that we keep on using #2x but I don't want my image stretched.
There is no specific image name conventions for iPhone 5 except for the Default-568h#2x.png image.
Your images will not be stretched when you use #2x on the iPhone 5, it is just the layout that will change.
Since there is no specific naming convention for the 4" screen (5th gen iPod touch and iPhone 5), you need to check the screen height yourself and load a different image.
if ([UIScreen mainScreen].bounds.size.height == 568) {
// 4" screen - load appropriate image
} else {
// regular screen - load standard image
}
This will work in portrait orientation.