Mayank Patel Mayank Patel - 1 year ago 67
Objective-C Question

How to handle Image sizes on different devices

Currently, i am doing this type of view, What is the best way so i can manage the image in different size of scale

this one

We have this sizes,

Device Points Pixels Scale Physical Pixels Physical PPI Size
iPhone 6 Plus 736x414 2208x1242 3x 1920x1080 401 5.5"
iPhone 6 667x375 1334x750 2x 1334x750 326 4.7"
iPhone 5 568x320 1136x640 2x 1136x640 326 4.0"
iPhone 4 480x320 960x640 2x 960x640 326 3.5"
iPhone 3GS 480x320 480x320 1x 480x320 163 3.5"

My Problem is image coming from server i want to handle this image in all type of device of Apple, I am using aspect fit in cell and when i am using 640 * 106 size then in i-phone 6plus this images are are not showing correctly size

Here's my code

CGRect frameIphone = (flag) ? CGRectMake(0, 0, self.view.frame.size.width, 106):CGRectMake(0, 0, self.view.frame.size.width, 106);

UIImageView *img = [[UIImageView alloc] initWithFrame:(iPad) ? CGRectMake(10,8,55,55) : frameIphone];
img.tag = indexPath.section;
img.contentMode = UIViewContentModeScaleAspectFit;
img.layer.masksToBounds = YES;

[img sd_setImageWithURL:[NSURL URLWithString:[objClass getImageForBackground]] placeholderImage:nil]; // URL Images

Answer Source

Its not that hard really, because what you doing is fixing the width/height of the view, what you need to do is calculate based on the screen ratio and give it correct height since the width is fixed based on the screen width, example if the screen is iphone 6plus, which have point of 736x414, and the image is 640x106, you do a calculation 736*109/640 (screen width * image height / image width, change with variable) = 125, thats the imageView height you should use, not 106 directly

You can set the image to the image view first then config it's frame also ok, so you can get the image height/width