-1

I need to reduce the size of UIImage captured from Camera/Gallery & reduce to size to max 200kb. The UIImage would then saved to a SQLite Database in the app.

I tried using UIImageJPEGRepresentation(image, compression); in while loop. but couldn't crack it.

Thanks for the help..!!!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Yash Soni
  • 164
  • 1
  • 4

1 Answers1

0

You need to scale the image down first, straight out of camera it will likely be too big. Something like 1000 pixels on the longest side might be enough, but you can play with it and see what works for you. Then apply JPEG compression to a scaled image.

Also the way JPEG works, it's pointless to run the algorithm over and over again. Just pick a good compression rate and run it only once.

On a tangent, in many cases where you need to save a large data blob in a database, you might find it more memory efficient to save the data into a separate file in the file system, and store the path in the data base.

SaltyNuts
  • 5,068
  • 8
  • 48
  • 80
  • Not necessarily if the image is still too large after compression. If there is a hard cap at 200 KB, you can make some estimations about whether or not the compression would do the trick, but you won't know for sure until you've done it and checked the result. For instance, if a new camera is put in with higher-def originals... – Stonz2 May 14 '14 at 15:47
  • What I mean is, if the image is still too large after compression, instead of compressing an already compressed image, it's better to apply a higher compression to the original. – SaltyNuts May 14 '14 at 15:49
  • Ah- good point. The asker would still potentially need to run the compression multiple times in their `while` loop, but instead of repeatedly compressing the same image, they could increase the `compression` value and compress the original until the desired result is reached. – Stonz2 May 14 '14 at 15:52