My application is downloading a set of image files from the network, and saving them to the local iPhone disk. Some of those images are pretty big in size (widths larger than 500 pixels, for instance). Since the iPhone doesn't even have a big enough display to show the image in its original size, I'm planning on resizing the image to something a bit smaller to save on space/performance.
Also, some of those images are JPEGs and they are not saved as the usual 60% quality setting.
How can I resize a picture with the iPhone SDK, and how can I change the quality setting of a JPEG image?
Best way to scale images without losing the aspect ratio (i.e. without stretching the imgage) is to use this method:
Add this method to your Utility class so you can use it throughout your project, and access it like so:
This method takes care of scaling while maintaining aspect ratio. It also adds indents to the image in case the scaled down image has more width than height (or vice versa).
To resize an image I have better (graphical) results by using this function in stead of DrawInRect:
Aspect ratio is taken care for automatically
For Swift 3, the below code scales the image keeping the aspect ratio. You can read more about the ImageContext in Apple's documentation:
To use it, call
resizeImage()
method:The easiest and most straightforward way to resize your images would be this