Unable to set a perfect image for swift spritekit background - ios

This is my first time to make a game with spritekit and swift , and I'm having a problem with a Scene size and the background image for the GameScene and the LaunchScreen. Currently, I'm using a screen size of
let scene = TitleScene(size: CGSize(width: 750, height: 1334))
I prepared 3 background images for the scene , main.png = 750 × 1334
main#2x.png = 640 × 1136, main#3x.png = 960 × 1685
I've read multiple swift game tutorials but I cannot find a solution for supporting the screen sizes for all iphones. When I set the background for the TitleSecen with
var bgImage = SKSpriteNode(imageNamed: "main")
self.addChild(bgImage)
bgImage.position = CGPointMake(self.size.width/2, self.size.height/2)
When I use the script above the screen gets centered and small. What size should I set the image for the background? The image for the LaunchScreen is also strange as well. I've there is a really good tutorial or a example for a noob like me it will be helpful!
I would love to here from you!

First of all, your image sizes are everywhere, and I don't think you understand how Xs work.
Basically, they are the ratios of which the resolution increases based on the device tipe (non-retina, retina, retina HD).
Let's take an example.
You need an image for a button. You support iPhone, from the iPhone 3gs to the 6 Plus. That means you have all 3 types of displays supported.
Your button is 50x50 points. A point = 1 pixel x resolution ratio
the iPhone 3Gs is non retina, it has a ration of 1 (1 pixel in a point), so your image should be 50x50 pixels => image#1x.png = 50x50 pixels
the 4, 4s, 5 and 5s are retina, they have a ration of 2 (2 pixels in a point), so your image should be 100x100 pixels => image#2x.png = 100x100 pixels
the 6 and 6+ are RetinaHD, they have a ratio of 3 (3 pixels in a point), so your image should be 150x150 pixels => image#3x.png = 150x150 pixels
You should read the iOS UI Guidelines
Secondly, in your case, the image is 750 x 1334. So image#2x should be 1500 x 2668 and image#3x 2250 x 4002.

Related

How to decide 1x, 2x and 3x image sizes based on the size of the corresponding UIImageView?

My query is in two parts:
1) plain and simple, how do we decide the 1x, 2x and 3x image sizes ?
I found this SO question closest
what-should-be-3x-images-of-100x100 size UIimageView or UIButton
but there is no accepted answer in this so I am a little confused if that is really how it is
So if I have a UIImageView of size, say, 17 X 21
So will my 1x be 17X21, 2x be 34X42 and 3x be 51X63 ?
2) If it is so what the sizes would be in case my UIImageView size also varies as per screen size.
So for iPhone 4s 320X480 if my UIImageView was 17X21, for iPhone 6 375X667 it would be 20X29
So how do I decide my image sizes (1x, 2x & 3x) which are fit for all device sizes?
PS:
Really a big thanks in advance, coz this might sound a very basic question but I have really banged my head a lot over it but can't find a concrete satisfactory answer even after searching and reading a lot.
You should think in points, not pixels.
1
If your UIImageView has the size of 17x21 points then you'll need to provide 3 images 17x21 pixels, 34x42 pixels and 51x63 pixels. The OS will pick the correct one depending on the pixel density of the device the app is running on.
2
One way to decide the size a UI element is using a software like Graphic. You can draw a rectangle representing the screen and the UI element only thinking in points. This should make things easier.
It is a bit late but I understand your problem.
First you need to know the difference between point and pixel.
The point is when you in Xcode.The pixel is when you have the actual size of iPhones screen as pixel.
In 4s, 1 px equal 1 point.
In 5, Se, 6 , 1 point 2 px
in 6s, 7s , 1 point 3 pixel.
I am gonna give you 2 different example to understand this.
Example 1
You want you image 100x100 for all iPhones in Xcode.So you should make :
1x size 100x100 pixel in Ps or Sketch.
2x size 200x200 pixel in Ps or Sketch.
3x size 300x300 pixel in Ps or Sketch.
Another example answers you question.Because you have aspect radio.That means you want your image to be scaled.
You have 17x21 point for 4s then it should be 17x21 pixel.
You have 20x29 point for iPhone 6. It should be 40x58 pixel.
You have something for 6s and 7s so it should be 3 X something.
Because you have aspect ratio and scaled.It does not matter as long as you keep 1x for 4s, 2x for 5, 6, SE, 3x for 6s and 7s.
So you have 40x58 instead 34x42 (17x21) x 2 for iPhone 6.
40x50 for better resolution which Apple recommend.When you use aspect ratio, it sometimes be hard to choose the image size.If you used 32x42 instead 40x58 , there is nothing wrong with this but resolution would be less than 40x58.
Hope It works.

Image sizes for iPhone are incorrect-

I have this image called image.png - I have created it in the 3 differen't sizes.
image#3x.png - 1001x132px
image#2x.png - 668x88px
image.png - 334x44px
What I dont understand is how come the #2x is 668px in width, when the iPhone 5&5S are only 640px wide?
I have designed the #3x using iPhone 6+ screen size.
Could someone please explain?
Let me explain you the difference between the iPhone screens, so you will understand the need of these #?x suffixes.
The first iPhone, and the two after it (3G and 3GS) had the standard display with dimensions w:320, h:480. With the introducing of iPhone 4, Apple also introduced the so called Retina display, which "packs" 4 physical pixels in 1 logical pixel (also called "point"), i.e.:
Now, even though the pixel dimensions of the iPhone 4 are w:640, h:960 (and iPhone 5/5C/5S w:640, h:1136), you still have screen size of w:320, h:480 points (respectively w:320, h:568 for 5/5C/5S). But because the coordinate system of the display uses points instead of pixels, the dimensions and position in points of all UI elements remains the same for every device.
What about the prefixes?
Now, the ratio between point and pixels is called "scale". UIScreen, UIView, UIImage, and CALayer classes have a property called scale, and when you load image with prefix #2x, the scale will be 2.0 and the display will fit 2x2 bitmap pixels from the image in one point. iPhone 6+ uses the #3x suffix because of the Retina HD display, which has scaling factor 3 and the image is afterwards downscaled from rendered 2208 × 1242 pixels to 1920 × 1080 pixels (points on the screen).
The downscaling ratio is 1920 / 2208 = 1080 / 1242 = 20 / 23. That
means every 23 pixels from the original render have to be mapped to 20
physical pixels. In other words the image is scaled down to
approximately 87% of its original size.
(source).
Required image sizes
For example if you want to have an image of green circle in a black square with dimensions w:100, h:100, you will need two more images: one with dimensions w:200, h:200 and one with w:300, h:300, as the system will scale them according to the suffix.
Where to go next
Here is some stuff to read (if I didn't explain it right or easy to understand). My advice: check the link above and the official Apple documentation for further knowledge:
Apple documentation: Supporting high-res devices
Apple documentation: Points vs. Pixels
#2x is using for retina display, like iphone 4, 4s, 5, 5s, 6, ipads ....
The image has 668px in width, I think it use for a view that could have extra area for view, like horizontal scroll view, tutorial pages, or for ipad ... ... or something like that ..
And yes, you're right about the width of retina display, it is just 640px.
Hope this may help.

Image.xcassets rendering strangely on simulator

I designed an image on Adobe Illustrator that has H: 100 px and W: 2000 px. When the device is in landscape left, this asset should run across the bottom the of screen and completely cover it from end to end.
As far as I know, the iPhone 6+ is 1080 x 1920. An asset with the width of two thousand should comfortably fit on the bottom, so I must be missing something big here.
I used Prepo to covert my 3x (the one with width of 2000), into 2x and 1x.
The figures are as follows:
3x: 2001 x 102
2x: 1334 x 68
1x: 667 x 34
This set:
Fits: 5s, 5, 4s
Too Short: iPad Retina, iPad Air, iPad 2, 6+, 6
I'm assuming I'm missing some critical aspect of image xcassets because I'm sort of at a loss.
Thank you, and I'm happy to update this with any other details needed.
First thing...
The iPad Air resolution is 2048x1536 and so your image of 2001 pixel width will not fit. Also, iPhone 6+ resolution is 1242x2208 so again that won't fit.
Second...
You shouldn't only be relying on the image size to fit to the screen. You haven't shown what the image is but I imagine that something of that size will be some sort of gradients or something that spreads across the bottom of the screen.
In that case you should be using a sliced image that will resize to be any size regardless of the device resolution.
Third...
If you do rely on the image size then what guarantee do you have that in the future there will not be a device with an even bigger resolution that will break the image again?
Hint... The near future is guaranteed to have a device with a bigger resolution that will break your display.
Summary
Use a resizable image that will stretch to the required size no matter what it is.
Use something like AutoLayout so that the frame of the image view is set by the screen size rather than being set by the image size.

Resizing an image without loss of quality for different screen sizes. What is the best way to go about it?

So I am currently having some images designed for my app (social stickers Facebook style/ Line) I'm not really sure what resolution the images should be. iPhone 5s is 320*568 pts so a perfect square panning the width of the screen would be 320*320 points and because 5s is retina that would be 640*640 pixels. Is that correct? For a pixel perfect image spanning the width it would be 640*640 pixels?
I saw that iPhone 6 plus is 414*736 pts which scales up by *3 so it's 1242*2208, so would a pixel perfect square spanning the width for the iPhone 6 plus be 1242*1242 pixels?
If I was to get my graphic artist to draw on a 1242*1242 canvas would I lose much quality scaling it down for the iPhone 6 and the iPhone 6 plus?
Not really sure how to go about this. Just want to make sure my square canvas rescales without loss of quality between the 6+,6 and 5. Any help would be greatly appreciated. Thanks!
When you rescale from higher pixels to lower pixels it never lose any quality, so in your case yes you must ask for iPhone 6+ size of images to your designer and you can rescale as per your need, you won't lose any quality for sure.
width for the iPhone 6 plus be 1242*1242 pixels?
Yes you are right, you will need 1242 x 1242 for iPhone 6+.
if you are making a universal app, you will need Retina and Unretina versions of each image for both iPad and iPhone. That is, four different images:
For every piece of art, you generate four sprites.
iPhone Unretina: image.png
iPhone Retina: image#2x.png
iPad Unretina: image-iPad.png
iPad Retina: image-iPad#2x.png
Except for backgrounds, make iPhone Retina art the same size as the iPad Unretina art.
Extract from this article: http://www.gameartguppy.com/how-to-size-game-art/

How to support various iphone screen sizes as of 2014?

I'm almost done building my first iPhone app and am trying to add a background image, and am finding it a little confusing, because nowadays there's like 3 or 4 different size screens among the different iPhone versions, with different resolutions to boot.
So while I'm aware of the whole image#2x.png thing, I still don't know what I really need. If I want my app to run on iPhone 4/4s, 5s/5c, 6/6+, how many different versions of a background image do I need, and at what sizes and resolutions?
I have googled around and have not found any cohesive answers up to date for 2014.
Also, if the iPhone 6 is 1334x750 #3x, does that mean I'm supposed to include a 4002x2250 background? And then for the 1920x1080 iPhone 6+ #3x, a 5760 x 3240 image? That's MASSIVE! I feel like I must be understanding this incorrectly.
If you want to support native resolution of iPhone 6/Plus, you need to add launch images (prior to iOS 8) or launch screen xib (iOS 8).
iPhone 4/4S: 640 x 960
iPhone 5/5S: 640 x 1136
iPhone 6: 750 x 1334
iPhont 6 Plus: 1242 x 2208
That means you need to prepare 4 launch images with above resolution if you want to support these devices. You can use iOS simulator to capture screenshots with different resolutions. Your app will run in compatibility mode on new resolution devices if it can't find the launch image of the specific resolution. compatibility mode means your view will be scaled to fit the new screen size when still have the same logical size.
EDIT:
I think op misunderstands what do #2x and #3x mean. The resolution of iPhone 6 is 750(pixels) x 1334(pixels), 326 pixels per inch. This is the REAL resolution. And 375(points) x 667(points) is the logical size if the native resolution is supported. iPhone 6 Plus's resolution is 1242(pixels) x 2208(pixels), 401 pixels per inch and the logical size is 414(points) x 736(points).
This is how images with different resolutions work on iOS device:
Let's say you want to run your app on iPhone 4s, iPhone 5/5S, iPhone 6/plus. First thing you should do is to provide 4 launch images to support native resolutions of these devices. When iOS launch your app, it will check if the app provides the right launch image to support the native resolution of current device. If iOS finds it, then use that in launch time and the logical size of screen is right, your app runs as normal. Otherwise, your app will run in compatibility mode in which all of the views will be scaled.
Suppose there is an image named foo.png in your app of which the logical size is 100(points) x 100(points). You want this image looks same in all the above devices. You should provide 2 versions of this image. One is 200(pixels) x 200 (pixels) and should be named foo.png#2x and the other is 300(pixels) x 300(pixels) named foo.png#3x. If you load this image with [UIImage imageNamed:#"foo"], on devices except iPhone 6 plus, the app will load the image named foo.png#2x. Otherwise the app will load foo.png#3x and sample it down to 300 * 84%(pixels) x 300 * 84%(pixels).
And if you load an image from an url and need to render it on the runtime. Let's say the size you get is {width:100, height:100}, the scale is 1.0. This means the REAL size of this image is 100 * 1.0(pixels) x 100 * 1.0(pixels. If you don't want it to be scaled, you need to calculate the logical size yourself. You do it like this:
UIImage *image = ... // you get it from an url
CGFloat scale = [UIScreen mainScreen].scale;
CGFloat width = image.size.width / scale;
CGFloat height = image.size.height / scale;
CGRect frame = CGRectMake(50.0f, 50.0f, width, height)];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
imageView.contentMode = UIViewContentModeCenter;
imageView.image = image;
[self.view addSubview:imageView];
I think the easiest way to do this is to use an image that fits on iPhone 6+ and then simply set UIImageView's contentMode to UIViewContentModeCenter. I think it's that one at least, the result should be that on iPhone 6+ you have a centered image but on the other screens you simply get a part of the image.
The downsampling of the iPhone 6+ means that an #3x asset covering the entire screen should be 2208x1242 (That is, this resolution is already 3x. The "real" resolution is 736x414). This is the largest resolution needed and you can then use the same image for #2x and #1x assets using the method I described.
You will need three versions of the background image(1x,2x,3x).A good practice to have images of the correct size is to make an image of a size that is 3 times the size of your view and then make the 2x and 1x versions by scaling down.
Apple has come up with a new concept called 'Size Classes' to support the different device sizes.To use size classes you will need the latest version of Xcode(Xcode 6). In the interface builder you can set different set of constraints for different size classes. This way it becomes easier to support all screen sizes. To know more about Size classes watch the WWDC 2014 video "Building Adaptive Apps with UIKit".
Which game engine are you using ?
If you are using cocos2d or cocos2dx then you can use background image of 960 * 640 .Below code will scale the images according to the screen size.
CCSize frameSize = pEGLView->getFrameSize();
CCSize designSize = CCSizeMake(480, 320);
vector<string> searchPaths;
CCSize resourceSize;
Utility :: isPad = true;
searchPaths.push_back("hd");
resourceSize = CCSizeMake(960, 640);
CCEGLView::sharedOpenGLView()->setDesignResolutionSize(designSize.width , designSize.height, kResolutionExactFit);
pDirector->setContentScaleFactor(resourceSize.width/designSize.width);
These are the different resolutions you will need for your background image.
iPhone 4/4S - 640 * 960
iPhone 5/5S - 640 * 1136
iPhone 6 - 750 x 1334 for portrait 1334 x 750 for landscape
iPhone 6 Plus - 1242 x 2208 for portrait 2208 x 1242 for landscape

Resources