0
votes

UIImage 1: Loaded from a file with the @2x modifier with size 400x400, thus UIImage 1 will report its size as 200x200

UIImage 2: Loaded from a file without the @2x modifier with size 400x400, thus UIImage 2 will report its size as 400x400

I then create 2 images from the above applying the code below to each

UIGraphicsBeginImageContextWithOptions(CGSizeMake(400,400), YES, 1.0);
[image drawInRect:CGRectMake(0, 0, 400, 400)];
UIImage *rescaledI = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Considering the above, can I expect the image quality for both resulting images to be exactly the same? (I am trying to determine if drawing a 200x200 retina image to a 400x400 non-retina context will degrade quality versus drawing the same image not loaded as a retina image)

2

2 Answers

1
votes

Just return the current image's size.

   UIImage *image1 = [UIImage imagedNamed:@"myimage.png"];
    //access width and height like this
    image1.size.width;
    image1.size.height;

UIGraphicsBeginImageContextWithOptions(CGSizeMake(image1.size.width,  image1.size.height), YES, 1.0);
[image drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height)];
UIImage *rescaledI = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Of course if you should replace image1 with whatever image you are trying to get the size of. Some switch statement or if statement should do the trick for you.

Never hardcode sizes/dimensions/locations etc. You should always pull that info dynamically by asking your image its size. Then you can change the size of your image without fear of having to locate the hardcoded size in your application.

1
votes

The image is always 400*400 pixels: the difference is that in a retina display 400*400 pixels cover less space, that is exactly half (200*200 Core Graphics points). If you are not applying any transformation to the image will stay exactly the same.

The code you wrote renders the image as is because you are overriding the device scale factor and setting it to always 1 (1 pixel to 1 point). You should use two images, one twice as big, if you want your image to cover the same amount of screen on both retina and non retina devices.