iOS Background Image is repeated in iPad? - ios

I've got an image with 1x , 2x and 3x types with resolutions 404*750 , 808*1500 and 1212*2250 respectively.
Here is my code:
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"bgr"]];
On my iPhone the background is not repeated and its good. but the iPad background is repeated 4 times.
Would you know why is this happening?

From Apple's reference
You can use pattern colors to set the fill or stroke color just as you
would a solid color. During drawing, the image in the pattern color is
tiled as necessary to cover the given area.
So in iphone device image is large enough to cover the area ... but in ipad it's not large enough to cover the area ... so it will repeat itself.
You can use image.xcassets for different image for iphone and iPad ...
see the screen shot
You can use different image for iPad with proper size to overcome this error

iOS will pick the image itself if you just follow the naming convention.
1x: my_image.png
2x: my_image#2x.png
3x: my_image#3x.png
1x iPad: my_image~ipad.png
2x iPad: my_image~ipad#2x.png

Related

Size of image to set as Background image in view controller

I am trying to put a background image on a UIViewController.However i cannot figure out the size needed because the image should support devices from iphone 4s to iPAD...Here are few questions:
1) Should i put those large image on image xcassets on 1x,2x,3x size,if so what should be the size of those images?
2) Or Should i Copy a high resolution image on the bundle itself again if so what should be the size?
We have differnet-2 sizes in assets like here:
(Only for iPhones)
What I found is that, you should opt for first option and 750X1333, 1080X1920 and 1242X2208 should be the 1x,2x and 3x.
You can add more by clicking on tick sign and give sizes like above we give for the iPhones.
Designer will give you proper sizes with respect to resolution to each ipad and iphone
Well it depends on what kind of image you want to display
If it's a solid or pattern image that won't have any different if scaling up / down then I suggest that you put it in image xcassets and use for all supported devices. You should put the largest size image, you can find all screensizes from this: http://iosres.com/
Otherwise, if it's a picture with details like a portrait or food dish, etc. You have to drop it manually with some photo editors like photoshop then add it to xcassets separately for iPhone and iPad
In place of 1x,2x & 3x images take single PDF image and place in image assets it ll fit for all comparability devices.
I think this code will help:
var backGroundImageView :UIImageView! //declare
///set image contentMode in viewDidLoad()
self.backGroundImageView.contentMode = .scaleToFill
However, you should check whether the image is in landscape or portrait orientation.
Image xcassets on #1x,#2x,#3x size will match iPhone 4S screen size automatically, #2x is for the iPhone 5, #3x for the Plus.
As for the iPad you can check the Apple Developers documents.

iOS: Preparing background images for applications

Mostly every iOS application has a view with an image as background. Is there any image sizing guide out there? For example here is an iOS screen designed in Sketch:
As you can see there is a background image. Now there are lots of Apple devices every application should support. The new iOS 10 supports all devices from iPhone 5 to iPhone 6s Plus. They have different screen sizes and resolutions. When creating Xcode assets, I am giving 3 background images with different sizes - #1x, #2x, #3x. What sizes should they be?
The way I see it you have 2 options:
In here you will find the resolutions of the iPhone's:
You don't need the #1 image since you don't support iPhone 4 and 4s (iOS 10).
#2 is for iPhone 5,5c,5S,6 and 6s so basically you can create #2 image of the highest resolution which is the iPhone 6 and this image will work well for the iPhone 5 family.
Or, you can create an image with resolution for each iPhone and using hard coded logic set the image for each phone.
i.e: if iphone5c { setImage("iphone5cImage") } etc etc..
The simplest solution is to create 1 image with the highest resolution. The #3 is the highest for the iPhone 6S+ and it will look amazing for the rest. Don't forget to set the image view as aspect fill.
Also, don't forget to check this thread: How to handle image scale on all the available iPhone resolutions?. It will give you clues of what exactly you are dealing with. TL;DR, It's the options I wrote.
The background images you only need to give are #2x and #3x, because #1x devices are now long gone in the dusty pages of history.
Speaking of #2x and #3x, the image resolutions you give to the developer should be the same with the highest resolution iPhone that uses that given size.
For #2x, that is the iPhone 6, which is 750x1334, and for #3x, the iPhone 6+ which is 1242x2208.
Down-scaling shouldn't be a problem because the aspect ratios of all iPhones that support iOS 10 are the same(16:9).
Note for Developer(s):
The UIImageView will then down-scale the images appropriately,
provided:
1. you created an image set with the provided #2x and #3x images,
2. correctly constrainted the UIImageView to the edges of the superview, and
3. selected the Content Mode of the UIImageView as Scale to Fill or Aspect Fill.
There is design nuance in full size background images. Mostly if the scale aspect fill good enough for different sizes you need to design only for the biggest device size after that the rest of them scale to fit. Sometimes some part of background needs to remain visible or if want to keep a low memory footprint for small device sets you need to create smaller alternatives.
Whenever you make a decision with the design size of asset you need to create #3x,#2x variants.
One more thing I need to point out about vector designs. If your design is made only with vectors you can choose pdf vector export. Storyboards can accept vector assets and they are doing very good when scaling in full backgrounds.
I use background images in my Apps. To solve this problem I use one image that has the resolution to cover all iPhones and all the iPads except the large one.
The image size is 2048x2048 points or 1024x1024 pixels at #2x to cover the 9.7 inch iPad.
The image is compressed JPG to keep the size down. Note that I allow it to scale for iPhone 6 Pluse (#3x) and 12.7 inch iPad Pro (#2x) as the quality doesn't seem to be affected.
I can justify the scaling for the larger devices, because if I provided image for the 12.7 inch iPad Pro, it will be 5464x4096 points (#2x) and 2732x2048 pixels and then the JPG compression would have to be so high (if I wanted to keep the size down), that the quality of the image was low anyway compared with scaling.
If you need high quality try both JPG and PNG for comparison, because the PNG becomes very large for complex images, but gives the best quality.
If you still have the same problem then you can try this one. For this you have to add only one image with good resolution and below code..
UIImage *bgImage = [UIImage imageNamed:#"art_background_star#3x.jpg"];
CGSize screenSize = [UIScreen mainScreen].bounds.size;
UIGraphicsBeginImageContextWithOptions(screenSize, NO, 0.f);
[bgImage drawInRect:CGRectMake(0.f, 0.f, screenSize.width, screenSize.height)];
UIImage * resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIColor *backgroundColor = [UIColor colorWithPatternImage:resultImage];
self.view.backgroundColor = backgroundColor;
There are 3 kinds of Apple Devices (iPhone and iPad) that is
Normal device which terms to 1 pixel = 1 point#1x (Older iPhone and iPad devices)
Retina device which terms to 4 pixels(2 x 2) = 1 point#2x (iPhone 5+)
Retina iPhone6 and iPad which terms to 9 pixels (3 x 3) = 1 point#3x (iPhone6+)
For iOs 10 that will not support iPhone 4s so you only require #2x and #3x images.
As you can see above attached image iPhone 6 also support #2x Scale so use image size for #2x is of 750*1334 and for #3x is of 1242*2208 with image mode.
Take a look at the page on documentation, there is Static Launch Screen Images, and you can catch sizes from there.
You can get a device screen size, using
CGSize screenSize = [UIScreen mainScreen].bounds.size // (Objective-C)
let screenSize: CGSize = UIScreen.mainScreen().bounds.size // (Swift)
And after you can programmatically select an image you want, from set of an images from the bundle. Or to make a big one image for resizing, using knowledges from documentation, and to resize an image accordingly. Or...your choice.
Different "sizes" #2x, #3x is scale.
And here is the nice explanation.

What is a good approach for scaling/drawing images for different screen sizes?

In my iPhone app, I have a control that takes on quite different sizes on different screens. For example, (in the iOS point coordinates)
3.5 inch devices (iPhone 4S): 242x295
4 inch devices (iPhone 5 series, SE): 242x383
4.7 inch devices (iPhone 6 series): 297x482
5.5 inch devices (iPhone 6 Plus series): 336x551
As you can see, these control sizes are not proportional.
The problem
This control has an image as its background. That particular image is important for brand identity and the custom appearance that my company's designer wants to go with. The image gives the appearance of a material and has a texture. It also has shadows within itself. I would like to apply this image on to controls of different screen sizes (my control sizes are determined at runtime according to available space, as Apple may come up with new screen sizes anytime).
My current solution
The designer makes separate PNGs for me for each screen size and I hard code it with onto my control using an if-else for screen size (after determining the size mathematically before hand). As you can probably tell, this is a horrible approach for robustness. I'm also looking to expand to iPad and having a better scaling system will certainly help.
An idea
I take an image that's the smallest unit of the repeating texture and apply it to my control with the scaling option that repeats it throughout to obtain a final image.
HOWEVER, I lose my shadows and rounded edges this way. (I tried simply using the largest image as well and the disproportionate scaling makes the rounded edges horrible)
I tried looking for solutions and most resources do not deal with such images. I simply cannot lose any part of this image and it should be fully applied to the control, shadows and corners included.
I apologize if any or all of this is naive or if I didn't look for answers using the correct words. This is my first time posting here at Stack Overflow, and I'm looking forward to hearing from you guys.
Thanks!
p
Edit:
This is applied to a custom UIButton based control to give the appearance of a card.
Edit 2: Wain seems to have suggested a perfect answer. I will try it and let everyone know the results.
I'd use tiling as you describe, and I'd combine that with changing the view layer corner radius and applying a shadow offset. In this way you can separate the important part of the image and make it nicely reusable and you can leverage the capability of CALayer.
Note that when you set the shadowOffset of the layer you should also look at the shadowColor, shadowRadius and shadowOpacity.
You can used Assets.xcassets for managing images in ios. you can make image in 1x,2x and 3x scale.
For example you want an icon of size 50x50 pixel then 1x should be 50x50,
2x should be 100x100 and 3x should be 150x150. then ios automatically take appropriate image from this set.
Hope this will help :)
The aspect ration of iPhone 5, iPhone 6 and iPhone 6P are almost same. however the aspect ration of iPhone 4 is different.
Here is the steps which helps you.
So you need one image which image is suitable for iPhone 5 and its
#2x, and #3x image and iPhone 4 and its #2x image,
i.e if you have image with 242x383 for iPhone 5 then you need images
with its #2x, and #3x images. and you need image which is compatible
with iPhone 4 size.
You need to set UIImageView's contentMode as aspectFit.
So the idea is, make iPhone 5's image and its #2x, #3x images and iPhone 4's image differently. or you can put all things in UIScrollView and for iPhone 4 set contentSize of scrollView is 568. and make different image for iPad.

Running iPhone app on iPad mini iOS 8 results in wrong icon size in UIActivityViewController

I have an iPhone app (does not support Universal) running on iPad mini iOS 8. But I find the icon size in UIActivityViewController is incorrect. How to solve this?
The issue can be reproduced simply initializing a default UIActivityViewController.
UIActivityViewController *avc = [[UIActivityViewController alloc] initWithActivityItems:#[#"hello"] applicationActivities:nil];
[self presentViewController:avc animated:YES completion:nil];
The incorrect behavior is as below:
https://drive.google.com/file/d/0B2C5utygT1vHSnlGUzRVdU5YSTA/view?usp=sharing
There are 3 things to note here:
image background,
image opaqueness,
image size.
iOS 7
Image background:
Image background should be transparent.
image opaqueness
The "visible part" of the icon should be non transparent aka opaque. Note that any color information won't be preserved:
image size
Because the image won't be scaled by the system if too small/big, you have to provide appropriately sized image. I found image size 120px x 120px to fit perfectly.
Note: this size also takes icon padding into account.
iOS 8
Image background:
Image background should be white to match the system UIAction icons but you can also use an arbitrary color.
image opaqueness
Same as in iOS 7, "visible" part of the icon should be non transparent aka opaque, however in iOS 8 color information will be preserved.
image size
I am using image with size 240px x 240px, but you can apply custom sized image because system will automatically scale-to-fill image if too small/big.
Wrap up
That said, if you want to support both iOS 7 and iOS 8, you have to have 2 versions of custom UIActivity icon image.
For iOS 7 you should use 120px x 120px sized image with transparent background. Note: find the size that best suits your needs.
For iOS 8 you should use custom sized square image with white background and "visible" part of an arbitrary colour.
Code example
#define isAtLeastiOS8 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
- (UIImage *)activityImage
{
if (isAtLeastiOS8)
{
return [UIImage imageNamed:#"activity_icon_ios8"];
}
else
{
return [UIImage imageNamed:#"activity_icon"];
}
}
Hope that helps!
Credit goes to #damirstuhec
You might be using the image size that is too big to fit in this area, try using multiple size images with 1x, and 2x and that can fix your problem for example convert the same image into 2 sizes for logo, 30x30 and 60x60 and name them as
30x30 icon goes as -> sampleLogo.png
60x60 icon goes as -> sampleLogo#2x.png
import these in the Images.xcassets under the same name (not two different images) and then try using this.
EDIT :
30x30 and 60x60 are just to give you an idea, also if you Application is compatible with iPhone 6 Plus, then you might also need to use a 3x image that in the given example be 90x90
These images need to have a transparent background and I recommend these sizes:
// iPadShare#2x should be 126 px, iPadShare should be 53 px, iPhoneShare#2x should be 100 px, and iPhoneShare should be 50 px. I found these sizes to work for what I was making.
Hope this will help :)

Can I use just 2 sets of background pictures for my app and still support iphone 5?

I have 2 files called image.png and image#2x.png sizes: 320x568 and 640x1136.
If I use those files as background, does this solve the dead space on the iphone 5? Will the iphone 5 load the #2x picture? I understand that in an iphone with 3.5 and lower the background picture will get cut but I don't really care about it.
I have a project that is for both iPhone 5 and the four. For my background image, I made a uiview that was the size of the entire view. I then hooked this up to my background jpeg file. Under mode in the attributes inspector, I selected scale to fill. Since the uiview is held to all sides of the view, it works for both. It looks great in either size screen. The jpeg is pretty high res, but I didnt need to place any #2x. Hope this helps you out.

Resources