When to use double size images - ios

I was thinking to write some quiz app where questions are represented as images.
My question is, for each quiz question (question.jpg), and thus a jpg file, do I have to create
a double sized question#2x.jpg file?
Is it necessary?
Doing this seems will increase size of my program so I was thinking when/if this is necessary to do?
PS. And in case I have to do it, I will just have to double in size each image manually and add to the project right (both original and double sized image)?
PPS. Just to add more info. The questions are located on the web site, I have to download them and add to my project manually (like resources). On web site there are no different versions of the same image. So, I have whatever is on the web site. Some images I noticed are 800x600 in dimensions but some are also in dimentions 500x400. So after I download these images, how shall I name them? Just with original names? and forget about the #2x extension? What's the best practice?
(if this will help my image view will probably be smth .like 310 in width). Do I have to modify them in size? What to do?

Apple's naming conventions of high resolution images can be found here:
Apple doc naming conventions
If you are developing in the way that you want to support old screen as well as retina screens. You can use xxxx.jpg for old devices as iPhone 3gs and iPad 1, and use xxxx#2x.jpg for retina displays. Where the aspect ratio needs to be the same but the #2x image needs to be twice as big.
In your case "my image view will probably be smth .like 310 in width", then the #2x image needs to be 620px in width and normal revolution 310px.
There is actually no need to have both image sizes in the app as you can use the same image and just scale it(If you really really need to have the old resolution supported).
Even if you add just a #2x, it will scale itself if someone on an old device installs your app. It may become a bit blurry but will still be quite ok.
If you are planning to use a lot of images in your app I suggest using some sort of web service where the user can download content that is to be shown. But that's just me. As the app will quite quickly become very large as images takes up quite a bit of space. Of course this all comes down to how many images you will have.
The drawback of using a web service is that the user much have an internet connection to be able to play. And download your content.(Most quiz apps I know of does use a web service for this.) This is a matter of taste.
If you do need to support normal and #2x here us a method you can use. This method will return the scaled image so you just need the normal one or the #2x one and then scale to the other size. This will at least help you a bit when it comes to getting either your app size down or your clients download time down.
If you are using .jpg's and scaling them upwards you can quite easily get a pixalated image as it is a lossy format. But if that's what you still want to do and maintain aspect ratio, this is one way to do it:
-(UIImage*)resizeImage: (UIImage *)imageToScale withScale:(CGFloat)theScale{
UIImage *image = [UIImage imageWithCGImage:[imageToScale CGImage]
scale:(imageToScale.scale * theScale)
orientation:(imageToScale.imageOrientation)];
return image;
}
Usage
[self resizeImage:[UIImage imageNamed:#"question.jpg"] withScale:0.5];
0.5 would double the size and 2.0 results in an image half the size as the original.
If you want a more complex method to set for instance set a specific target size just say so and I'll edit this answer.

Related

iOS ideal image resolution

I'm having a real hard time understanding this, but let's say I have an iOS app for both iPad and iPhone and I want to download an image from a server and display it in full screen.
I have read that the iPad pro has a resolution of 2732x2048 and if we want to display an image in fullscreen we would need to download the image with this size right? However, I also read that the image should never be over 300KB. I was not able to bring an image with this size under 2MB (I used JPEGmini for example to reduce size).
And I don't think that iPhone user would need to download such a huge image, so my question is: what resolution should my images be on the server and how can I manage to keep them in a rational file size. Also should I upload multiple images for different devices? If so, how many and at what resolutions?
Isn't the problem merely that you are holding incompatible beliefs? This is the belief that is giving you trouble:
I also read that the image should never be over 300KB.
Let go of it.
Clearly it is right to say that the image should be no larger than needed for display. But an image to be shown as a 3x scale image on the iPad pro needs to be 2732x2048. So that's that.
(You could, alternatively, use an image 2/3 of that size and show it as a 2x scale image. It wouldn't look quite as good as the 3x scale image, but it might be acceptable.)
On a smaller device, yes, you should scale down the image in code, so that you are not holding in memory an image larger than needed for display. But in this case, you need the large image for display.

Download #2x and #3x images from server to Tableview

I have a tableview that is populated from images on my server that I download. My question is How would I go about getting different images for the different resolutions for the devices? 6 vs 6plus etc?
Do I write code that is device specific? e.g
if device is equal to 6 load #2x images 'from #2x url'
else if
device is equal to 6 plus load #3x images 'from #3x url'
and so on.
Or is there a more efficient way of going about this?
You've got several options, depending on the back-end technology stack you're connecting to.
Options if you're using a custom server that you've built yourself, or have control over the code:
Add device scale detection into your image service that checks the inbound user-agent for device scale, and serves the appropriate image for that device. This is hands down probably the simplest solution to implement if you manage your own back-end.
Write into your API a url path param that serves the appropriate image scale; you can then use the UIScreen class (UIScreen.mainScreen().scale) to adjust your target URL accordingly, e.g.:
https://api.yourbackend.service/images/{imageid}/#2x.jpg
This is arguably more difficult to implement, but it's also a lot more robust (doesn't rely on potentially changing user-agent strings), and it's a lot cleaner when logging or querying analytics.
You could probably also use something like retina.js to handle this on your back-end, though I can't tell you whether or not it will work heedlessly (haven't tried it).
Options if you're using a CDN image host:
Use their built in HiDPI support (Cloudinary, for example, provides this -- read about it here).
If your CDN doesn't support this, switch to one that does ;)
You are coming at this exactly backwards.
A table view cell's image view is tiny. You are not going to be showing these images at full size anyway. So there is no point whatever downloading larger and larger images only to be displayed at tiny sizes. That's a massive waste of time, bandwidth, and ultimately memory (if you really try to display large images in every cell, you will run out of memory and crash).
If you have a choice of image size to be retrieved from the server, you should be doing just the opposite of what you are suggesting: download a thumbnail of your image suitable for display in the table view. If the thumbnail is twice the size of the image view, it will look good at all resolutions with minimal waste of memory.
If this app is about also fetching the real full-sized image, outside your table view, you can do that later when requested by the user.

Do i need to return back the image dimensions for ios in json response?

I've built a website that stores images in 4 different sizes on the file system. It keeps the origional file size, medium, thumb, small image and resizes them as they upload.
We then created the web api to allow us to build the ios/android apps.
while building the apis our overseas developer then asked to provide the image dimensions along with the image file name. it took a lot of work regarding calculating the image sizes and saving them to the database.
{ "imagefilename" : "someimage.jpg", "LargeimageHeight" : "1000",
"LargeimageWidth" : "500", "mediumImageHeight" : "500",
"mediumImageWidth" : "250",
"smallImageHeight" : "100", "smallImageWidth" : "60" }
with the amount of image sections we have in the website made it a task and i want to know if this is really nessesary. Im not an ios developer but not really sure as to why the additioanl dimensions of the image need to be saved and or really needed.
i thought the ios has the image feature (#3x) (#2x) (#3x) ??
or do i use 1 specific folder image size for ios ??
does this always happen in the ios world for images that the code needs to know the image dimensions for the best result?
thanks
If your iOS application show images in flow layout (like pinterest style) you must return image dimensions. Otherwise I don't think its nessesary
Do you need the return the image dimensions:
Short answer: no. You can load the image using UIImage, and call image.size.height and image.size.width.
Long answer: maybe, depending on the interface design of your application, and if every image has different dimensions, it might take a performance toll to load the image into memory before being able to get its size (and probably do autolayout and have it render on screen). You might have issues with scrolling speed, but it will depend on the specific design and requirements.
What is the #2x #3x iOS image feature
iPhone and iPad screens come in different pixel densities (non-retina, retina and the retina iPhone 6+ screen). If you have a local image, stored on the phone, it can preload the image with the right scale automatically, by just providing the base name of the image, if you add the right suffix to each asset like name#2x, etc.
Sadly, it doesn't work the same for remote images see this answer. You will have to check yourself for the scale and load the right image, or load one big file, and let it be resized on the device.
You should consider providing dimensions inside your API response.
Think design:
When you scale your app to have many images at once, or when you distribute your app to multiple devices, you may realize the advantage. Your iphone 4 may end up downloading the image (worse, images) intended for iPhone 6S, and so on. If the reverse is true, it's even scary to think about.
How about the device asking for the image it's best suited for?
As for scaling of images, Bram De Geyter is correct - you can render whatever resides on the device, not fetch the correct one from the server. So having your API giving you the right dimensions is the only solution.
I have described the whole image loading from API approach in my blog here.

Adding iOS #1x, #2x, #3x PER device, how to?

I recently asked a question about why we use the 3 different image types listed above, and got good/detailed responses. It also lead me to question something else...
Assume I am working in XCode (with SWIFT) and have the three image sizes #1x, #2x, #3x for a particular say UIImage on an iPhone. Now, I want it to look nice on iPad, so I add some scaling. However, this will change the sizes of the 1x, 2x, 3x images I need for the iPad, so I need to make 3 more to compensate for that new size. Now how will XCode differentiate between the two? Do I need to check what size screen I am using and then choose the images?
I read that assets could help me, but that only deals with iPhone vs. iPad, what if I scale my images across iPhone 4, 5, 6 and iPad. That's a lot of different sizes and #x images, and I read Apple doesn't like device checking so I'm not sure how that would happen.
I hope this makes sense. I get the #1x, 2x, 3x per device, but if my app is being designed for universal device use, then how do I manage all of these different sizes in code for the proper device?
You really should use image asset catalog. It makes it almost too easy. Here is the definition Apple gives so you know it's not hearsay:
Image sets: Used for most types of images, an image set contains all the versions, or representations, of an image that are necessary to support various devices and scale factors.
And it goes on further:
Each set in an asset catalog has a name. You can use that name to programmatically load any individual image contained in the set. To load an image, call the platform specific class method, passing in the name of the set that contains the image. The OS will load the image from the set that is most appropriate for the current scale factor
So there really is not much for you to do. I don't know where you heard the opposite
See Documentation here
You need to check the size of the biggest image, if you are using an iPad figure out the size of the UIImageView frame. For instance if it is 400*400pts then your image will be 1200*1200. Just use a single image. I don't think you need to include the #2x and #1x. The 1200*1200 covers your bases for the largest possible image and scaling down will still have plenty of pixels left over for the full #3x resolution on other devices and so on. This is how I understand it anyway. I also wondered this myself and only found #1x #2x #3x useful for things that I didn't scale up as the screen size increased such as some buttons.

iOS- Including different image sizes for different display sizes

I am new to iOS programming and programming in general so this will probably be a fairly easy question to answer for someone who is experienced.
I am making a game using sprite kit and I need to include different background image sizes for the different retina display sizes on the iPhone 4 and 5. I am using a graphics package to create the images in .png format then adding them into the project, the issue I have is that, if I make a 640x1136 size image, it works on the 5, and if I use a 640x960, it works fine on the 4 but leaves blank space around the edge on the 5. (I am running it on the simulator)
If I include two identical images with different names, one for each device, how can I load the right one in? Do I only need high resolution image and can use some code to change how it loads the image in, so that it covers the whole screen without pixelation or loss of quality on both devices?
Any help or advice is appreciated. I apologise if this is a simple question, thanks for your time.
Note:
I found out plenty on the internet about using the #2x suffix for high resolution images, but that's not what I'm looking for. I know how to code for different resolutions, just not two different screen sizes with the same resolution, if that makes any sense.
If you're on iOS 7 SDK which you most likely are, make use of the .xcassets catalogue. It has options for different screen sizes, put the different versions of your image there. And then load image in code.

Resources