SimplyKiwi
SimplyKiwi

Reputation: 12444

CGRect in Pixels?

Pretty much I want to properly have the size of a button in my app converted to the amount of pixels for its width and height.

So lets say it has a width of 50 and a height of 100, on a retina display device I want it to return a width of 100 and a height of 200. On a non-retina device it should return a width of 50 and a height of 100.

And it should work the same way on the iPad. Anyway, how would I do this? Would I use the UIScreen's mainScreen scale, or is there another way?

Thanks!

Upvotes: 4

Views: 3468

Answers (2)

Henri Normak
Henri Normak

Reputation: 4725

Although I agree with the previous answer in that you should generally ship two sets of assets instead of resizing one on runtime, you can still do that.

Scaling an UIImage to the correct scale of the screen would indeed mean using UIScreen's scale property. However in future interests it might make more sense not to use [UIScreen mainScreen], but instead determine the view the image will get displayed in and obtain the screen from there.

All in all, it would probably make more sense to not mess around with creating a separate image, and letting the OS handle its' own scaling. If you know the asset is @2x you could use + (UIImage *)imageWithData:(NSData *)data scale:(CGFloat)scale to create the object and define the scale in there, this way the system will know the scale and will handle the resizing properly even if displayed on a @1x screen.

Upvotes: 0

rmaddy
rmaddy

Reputation: 318944

Everything you put on the screen in measured in points, not pixels. A non-retina iPhone is 320x480 points. A retina-iPhone (3.5") is 320x480 points.

This is true on all devices. Don't worry about pixels, worry about points.

  • 320x480 (3.5" iPhones/iPod touches)
  • 320x568 (4" iPhones/iPod touches)
  • 768x1024 (iPads - all)

Your button would be measured as 100x50 on all devices.

Update:

When working with images you need to create two sets of images.

  • image.png would be the "normal" size of say 100x50
  • [email protected] would be the "retina" size of 200x100

By providing the two images, the OS grabs the proper one. Your code still measures everything in points. So the 200x100 retina image will be shown as 100x50 points on a retina device.

Upvotes: 5

Related Questions