Adding images just for Retina - ios

Is it possible to target users that only have retina displays/have a particular version of iOS? If not, do I need to include non-retina images in my project? I know that for the application icon it needs a non-retina version, but for all of my custom graphics, such as UIBarButton icons, etc. do they all need to have both a retain (test#2x.png) and a regular (test.png) version?
Also, how should I go about scaling? Currently I'm creating say a larger image, like 100x100px and then scaling it down with the following code:
// grab the original image
UIImage *originalImage = [UIImage imageNamed:#"test.png"];
// scaling set to 2.0 makes the image 1/2 the size.
UIImage *scaledImage =
[UIImage imageWithCGImage:[originalImage CGImage]
scale:(originalImage.scale * 2.0)
orientation:(originalImage.imageOrientation)];
Does this maintain a higher resolution than if I re-sized it to 50x50 pixels in photoshop and then added the file into my project as is?

You cannot specify users device.

You'd better add two versions for all images into your app.
Because you could not know whether the device is retina or not.

Related

Show image of 10000x8000 pixels in iOS app

I'm trying to show image in my iOS APP.The image is of 10000x8000, which is much higher than iPhone's screen resolution, if I add it to UIImageView ,the APP will receive memory warnings and lead to crash.Can any one give me a advice about how to deal with it? Thanks a lot.
This is the sample app from apple which displays large size image, it uses TileImageView scale the image and reuse tiles according to present zoom.
https://developer.apple.com/library/ios/samplecode/LargeImageDownsizing/Introduction/Intro.html
Or if you simple want scale down the image, you could use this.
+ (UIImage*)imageWithImage:(UIImage*)image
scaledToSize:(CGSize)newSize;
{
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
This is just a side note and not an answer.
#AmitTandel's answer will surely help you but,
remember, you should not always request for this high resolution image instead you should implement a logic which stores this high resolution image to somewhere after reducing it using #Amit's answer. And then at the time of re-requesting for the same image url, you should first look up in to your stored directory, if that image not exist only then you may request for an image.
But yes, I'm not suggesting you to follow my answer as you will require to download this very high resolution sized image (which is actually none of use), so if you're getting this image from your server, you can ask them to give you reduced size for images. I've heard that, they can reduce images to different sizes to help apps showing specific images at variant places.

iOS UIImageView - Top of Image Chopped Off?

I'm new to iOS Development, I'm using a UIImageView to display an image. I've made a 320x480 and a 640x960 image called "red.png" and "red#2x.png".
No matter how I scale or align the UIImageView, the image always chops off half way at the top.
Is there something I'm meant to do to combat this, as I thought those resolutions were correct?
The UIImageView is sized at 320x568 to fill the storyboard.
Thanks :)
My comment above is hard to read, and it's probably an answer:
myImageView.contentMode = UIViewContentModeScaleAspectFit;
Try changing to image name as follows:
self.colourImage.image = [UIImage imageNamed: #"red.png"];
You should not directly specify '#2x' in image names.
It is automatically used if a device supports retina display.
That is why you need to create 2 sets of images; one for non-retina and one for retina (using #2x) and the OS selects the correct one for you.

UITabBarItem Image Size

I am making images for my UITabBar. I am making them of size 60x60, because that's what retina screens use. However, when I use that size, it shows up too big in the bar, so you can only see part of the image. When I reduce it down to 30x30, it works, but that size is supposed to be for non-retina displays. Why does it not show up properly when I use 60x60?
You probably may have to rename your retina image to imagename#2x.png.
You were getting this behavior because you were supplying a high-resolution image, when iOS was looking for a standard resolution image.
iOS automatically selects the appropriate image size for you, depending on the resolution of the accessing device. So you will be responsible for supplying a "standard resolution" image, for non-retina devices, and a "high resolution" image, for retina displays. The way you do this in iOS is to append "#2x" to the end of your filename, but before the file extension, like this:
my-image.png // for non-retina displays (Ex: 30x30 dpi)
my-image#2x.png // for retina displays (Ex: 60x60 dpi)
my-image#3x.png // for retina displays(plus editions) (Ex: 90x90 dpi)
Then, when you are referencing files in your XCode project, you only need to supply the filename to the standard resolution (e.g, "my-image.png") and if the accessing device has a retina display, then XCode will automatically select the file with the "#2x" suffix for you. This is very convenient, because it saves us developers from having to detect whether or not the device has a retina display, and which image we need to supply.
Here is a code example:
// Select an image named "my-image.png"
UIImage *img = [UIImage imageNamed:#"my-image.png"];
// If the device this code is run on is a retina device,
// then Xcode will automatically search for "my-image#2x.png" and "my-image#3x.png"
// otherwise, it will use "my-image.png"
You can read more on the subject via the Apple Developer's Site: Optimizing for High Resolution

UIImage resize performance and quality issue

I'm working with UIImage and like everyone else have to deal with retina and non-retina display adaptability. As for as I know, retina display requires double pixels.
I'm wondering if I could simply use a large image with the same width/height ratio, just resize it smaller to adapt all device?
For example, I made a original image with size of 200*200 pixel. Now I want to use it in application as 20*20 pixel, and 80*80 pixel (two situations). Then I have to make four copies like img2020.png, img2020#2x.png, img8080.png and img8080#2x.png
So if I want to use it in three situations with difference size, I have to store 6 copies. Can I just use UIImage's resize function to do this? I've tried a bit but cannot figure out it's quality and performance.
Any ideas? Thanks a lot :)
All native API suppose you to use image.png and image#2x.png, so it may be difficult sometimes to use just one image and scale it depending on retina/non-retina. Moreover using retina graphics on non-retina devices lead to more extensive use of these devices' resource causing battery drain. And, of course, if you have many images, that will decrease performance of your application. In other words there are reasons to use double set of images and you should better use it instead of one large image being scaled.
You don't need to make 6 copies. You should use the size 200*200 pixel. And set the property contentMode of imageview to aspectFit. Or you can also use below function and change the size of images at run time.
-(UIImage *)Resize_Image:(UIImage *)image requiredHeight:(float)requiredheight andWidth:(float)requiredwidth
{
float actualHeight = image.size.height;
float actualWidth = image.size.width;
if (actualWidth*requiredheight <actualHeight*requiredwidth)
{
actualWidth=requiredheight*(actualWidth/actualHeight);
actualHeight=requiredheight;
}
else
{
actualHeight=requiredwidth*(actualWidth/actualHeight); actualWidth=requiredwidth;
}
CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight);
UIGraphicsBeginImageContext(rect.size);
[image drawInRect:rect];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
I made some comparisons before. Leaving iOS handle the resizing causes lower quality, and really unacceptable sometimes.
I feel lazy sometimes, my approach is to run it with the retina version, and if it looks bad, I will create a low-res version.
If you're writing an iPhone-only app, most of iPhones on the market has retina, so I don't think you should worry about non-retina version. Just my opinion though.

iOS image sizes for iPad and iPhone

I have developed an small iOS app, where i have image named bg.png which is of dimension
1024 * 768 for iPad.
Now i have many images which has been created for iPad size. Now i need to make support of this app in iPhone, for that weather i need to create same set of images agian for iPhone size,
568 * 300 for iPhone.
or there is another way to do this?
Scaling down the iPad image assets will destroy UX on iPhone. Also images like icon, splash screen usually contain company logo. Scaling down will tamper the look of the logo and overall image. Better way is to create separate images for iPhone form factor. Trim the png files using http://tinypng.org/ to keep binary size low.
Cheers!Amar.
You can use this code to re-size the image by following code,
CGSize newSize = CGSizeMake(568, 300);
UIGraphicsBeginImageContext(newSize);
[yourIpadImage drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
newIphoneImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
//UIGraphicsBeginImageContext(newSize);
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
You have option to change the Size of Your Image
sathiamoorthys solution is a difficult way or rescaling your image. You can do that by simply creating a UIImageView, initialize it with a UIImage and then change its frame.
Note that your image will look scaled/distorted that way.
follow this:
open the image in preview.
go to tools > adjust size
put in whatever size you want.
save the image as a different name.
yes
you should create duplicate and resize them for iphone. Using same images for iphone will bring memory issues because the images are unnecessarily big for iphone.
Use any software to resize them or you can do this using preview also as Nikita described above
If you are doing this to create universal app then you must postfix ~ipad in the name of the image file.
Please visit this link, May help you and solve your issue.
There is the some tips like:
Propotional scale,
Resize
If you want your images to show up unscaled, you are going to need an additional image with the correct size.
So supporting both iPad with and without retina screens would require one image of 768x1024 and one of 1536 x 2048. For iPhone 3.5" you would need 960 x 640 when it is a retina screen or 480 x 320 when it is non-retina. For iPhone 5 (4" screen) you would need 568 x 320.
If you use UIImages method imageNamed: there is help from Apple. It loads on retina devices that method looks for the the image you specified with the postfix '#2x'. So you can simply code:
UIImage * myImage = [UIImage imageNamed: #"myImage"]
If you make sure you project contains myImage.png for non-retina devices and myImage#2x.png for retina devices the right image gets loaded at runtime.

Resources