1

We are coding in iOS 7.x and I came to the following code snippet:

if ( condition ) {
  if (UIScreen.mainScreen.scale > 1) {  
            self.canvas.image = [myImage imageScaledToFitSize:CGSizeMake(30,30)];             
       } else { 
            self.canvas.image = [myImage imageScaledToFitSize:CGSizeMake(60,60)];       
        }
}

Is this the proper way to code? If this "retina" check is the way to go, then it should be placed everywhere in the code.

If this UIScreen.mainScreen.scale is not correct, could you please point the correct way to go for retina/non-retina display handling?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
cateof
  • 6,608
  • 25
  • 79
  • 153

1 Answers1

1

Seems a bit hardcoded; better would be:

if ( condition ) {
    CGFloat scale = UIScreen.mainScreen.scale;
    self.canvas.image = [myImage imageScaledToFitSize:CGSizeMake(60.0 / scale, 60.0 / scale)];
}

However it's not clear to me what the 60 signifies...

Droppy
  • 9,691
  • 1
  • 20
  • 27
  • Sizes are points in the interface, which are then translated to pixels – cateof Aug 06 '14 at 09:58
  • 1
    I suspect @droppy's point was concerned maintenance: where did a value of 30points come from? What does it signify. Ideally this should defined as a constant rather than being a literal as here. – marko Aug 06 '14 at 10:00
  • The code in this answer is backwards from what is posted in the question. It should be `60 / scale`, not `30 * scale`. – rmaddy Aug 06 '14 at 15:47
  • @rmaddy indeed it is! I'll fix it, however I'm not sure the OP is too interested anyway. – Droppy Aug 06 '14 at 17:20