title: How to downscale a UIImage in IOS by the Data size
body: <p>I am looking to downscale a <code>UIImage</code> in <code>iOS</code>. </p>
  
  <p>I have seen other questions below and their approach on how to downscale the image by size. 
  <a href="https://stackoverflow.com/questions/10334581/resizing-images-objective-c">Resizing Images Objective-C</a><br>
  <a href="https://stackoverflow.com/questions/4712329/how-to-resize-the-image-programatically-in-objective-c-in-iphone">How to resize the image programmatically in objective-c in iphone</a><br>
  <a href="https://stackoverflow.com/questions/2658738/the-simplest-way-to-resize-an-uiimage">The simplest way to resize an UIImage?</a></p>
  
  <p>These questions are all based on re-sizing the image to a specific size. In my case I am looking to re-size/downscale the image based on a maximum size.</p>
  
  <p>As an example, I would like to set a maximum <code>NSData</code> size to be 500 KB. I know that I can get the size of the image like this:// Check the size of the image returned</p>
  
  <pre><code>NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
  
  // Log out the image size
  NSLog(@"%lu KB",(imageData.length/1024));
  </code></pre>
  
  <p>What I would like to do is some form of loop here. If the size is greater than the maximum size that I set, I would like to scale down the image slightly, then check the size. If the size is still too large scale down again slightly then check again, until it is lower than the maximum set size.</p>
  
  <p>I am not sure what the best approach for this is. Ideally I do not want to scale down the image to a specific size all the time, but only slightly scale down the image each time. That way I can have the largest (size w/h) of the image itself and at its maximum size  (bytes). If I scale down slightly only at a time, what would be the best way to accomplish this?</p>
  
  <p><strong>EDIT</strong>
  To confirm, I am looking to re-size the actual image but re-size the image so that it is smaller than the maximum <code>NSData</code> Length.
  For example:<br>
  -Check the <code>NSData</code> Length<br>
  -If above the maximum I want to pass the UIImage into a method<br>
  -Then loop through this method slightly re-sizing the actual image size each time<br>
  -Until it is under the maximum <code>NSData</code> length, then return the image?</p>