How to Handle Image Scale on All the Available iPhone Resolutions

How to choose 1 UIImage size for all iPhone resolutions?

Use 1242px image and downscale it. To get the best possible image quality set yourUIImageView.layer.minificationFilter = kCAFilterTrilinear. You wont see any difference in the image quality when using trilinear filtering.

Another options is to downscale the image before using it:

extension UIImage {
public func rescale(width: CGFloat, _ height: CGFloat) -> UIImage {
let newSize = CGSizeMake(width, height)

UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
drawInRect(CGRectMake(0.0, 0.0, newSize.width, newSize.height))
let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

return scaledImage
}
}

let myImage = UIImage(named: "myimage").rescale(newWidth, newHeight)

Scaling an image from the iPhone - iOS App

Setting the image view's content mode like this:

myView.contentMode = UIViewContentModeScaleAspectFit;  

will preserve the aspect ratio.

How to make a fullscreen image for all resolutions

Here's my solutions. It might not be the best, but I guarantee it will work.

  1. Make copies of your original image, and resize those copies for different screen sizes. For iPhone 4, you need 320x480 @2x, 320x568 @2x for iPhone5, 375x667 @2x for iPhone 6, and 414x736 @3x for iPhone 6+. Remember the unit is point not pixel. Here's a link about the detail iPhone screen sizes:iPhone Screen Sizes

  2. Detect your screen size in run time and load different images to fit the screen. You can do this by the add the following code as a category to UIImage class.

Code:

#define IPHONE44S       480
#define IPHONE55S 568
#define IPHONE6 667
#define IPHONE6PLUS 736

#import "UIImage+ZCDynamicUImageSize.h"

@implementation UIImage (ZCDynamicUImageSize)

+(UIImage *)dynamicSizedImageWithName:(NSString *)name{
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
UIImage *image;
if (screenHeight == IPHONE55S) {
image = [UIImage imageNamed:[NSString stringWithFormat:@"%@-568",name]];
}
else if (screenHeight == IPHONE6){
image = [UIImage imageNamed:[NSString stringWithFormat:@"%@-667",name]];
}
else if (screenHeight == IPHONE6PLUS){
image = [UIImage imageNamed:[NSString stringWithFormat:@"%@-736",name]];
}
else{
image = [UIImage imageNamed:[NSString stringWithFormat:@"%@-480",name]];
}
return image;
}

  1. Change the name of all your images with name followed by it's height. For example, your image name is "MyImage.png", and it's the image for iPhone5, and change it's name to "MyImage-568@2x.png".

  2. Last, load your image by using the method created on step 2, then center the image to the center of the view.

supplying the right image size when not knowing what the size will be at runtime

The usual solution is to supply three versions, for single-, double-, and triple-resolution screens, and downsize in real time by redrawing with drawInRect into a graphics context when the image is first needed.

I do not know what the image height and width will be at runtime, because of different screen sizes of various iPhones. For example each image will be 100x100 display pixels on 5S, but 130x130 on 6+

Okay, so your first sentence is a lie. The second sentence proves that you do know what the size is to be on the different screen sizes. Clearly, if I tell you the name of a device, you can tell me what you think the image size should be. So, if you don't want to downscale a larger image at runtime because you don't like the resulting quality, simply supply actual images at the correct size and resolution for every device, and use the correct image on the actual device type you find yourself running on.

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

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+.



Related Topics



Leave a reply



Submit