7

I'm trying to upload 256 x 256 images from my iOS app. I've resized an image to CGSize(width: 256, height: 256), but it gets uploaded as 768 x 768. This makes sense, because on my iPhone X, 1 point is 3 pixels.

This isn't consistent though. On some iPhones, 1 point is 2 pixels.

How do I figure out to use CGSize(width: 128, height: 128) or CGSize(width: 86, height: 86)?

1
  • Show your code for creating the image. Are you using UIGraphicsBeginImageContextWithOptions? Look at the documentation for its parameters.
    – rmaddy
    Commented Feb 12, 2018 at 0:06

2 Answers 2

9

The direct answer to your question is to use the scale property of UIScreen.

But you probably don't need that depending on how your image is created. Given your tags, it is likely you are using UIGraphicsBeginImageContextWithOptions. Note its third parameter of scale. If you pass 0 you get the device's scale. But in your case you want a scale of 1.

6

Scale is points / pixels. Get Scale in swift 5

let myScaleVariable = UIScreen.main.scale

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.