I am looking to downscale a UIImage in iOS.
I have seen other questions below and their approach on how to downscale the image by size. R
Besides the maximum size you also need to choose a minimum size as well as decide on performance. For example, you could check the size of UIImageJPEGRepresentation(image, 1.0). If too big, do you then check at 0.95 or 0.1?
One possible approach is to get the size of UIImageJPEGRepresentation(image, 1.0) and see by what percent it is too big. For example, say it is 600kB. You should then compute 500.0 / 600 which is roughly 0.83. So then do UIImageJPEGRepresentation(image, 0.83). That won't give exactly 500kB but it may be close enough.
Another approach would be to start with UIImageJPEGRepresentation(image, 1.0). It it's too big then do UIImageJPEGRepresentation(image, 0.5) If too big then go with 0.25 but if too small go with 0.75. Keep splitting the difference until you get within an acceptable range of your desired size.