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?
I just wanted to answer that question for Cocoa Swift programmers. This function returns NSImage with new size. You can use that function like this.
A couple of suggestions are provided as answers to this question. I had suggested the technique described in this post, with the relevant code:
As far as storage of the image, the fastest image format to use with the iPhone is PNG, because it has optimizations for that format. However, if you want to store these images as JPEGs, you can take your UIImage and do the following:
This creates an NSData instance containing the raw bytes for a JPEG image at a 60% quality setting. The contents of that NSData instance can then be written to disk or cached in memory.
The above methods work well for small images, but when you try to resize a very large image, you will quickly run out of memory and crash the app. A much better way is to use
CGImageSourceCreateThumbnailAtIndex
to resize the image without completely decoding it first.If you have the path to the image you want to resize, you can use this:
More details here.
If you have control over the server, I would strongly recommend resizing the images server side with ImageMagik. Downloading large images and resizing them on the phone is a waste of many precious resources - bandwidth, battery and memory. All of which are scarce on phones.
I developed an ultimate solution for image scaling in Swift.
You can use it to resize image to fill, aspect fill or aspect fit specified size.
You can align image to center or any of four edges and four corners.
And also you can trim extra space which is added if aspect ratios of original image and target size are not equal.
There are examples of applying this solution below.
Gray rectangle is target site image will be resized to. Blue circles in light blue rectangle is the image (I used circles because it's easy to see when it's scaled without preserving aspect). Light orange color marks areas that will be trimmed if you pass
trim: true
.Aspect fit before and after scaling:
Another example of aspect fit:
Aspect fit with top alignment:
Aspect fill:
Fill:
I used upscaling in my examples because it's simpler to demonstrate but solution also works for downscaling as in question.
For JPEG compression you should use this :
You can check out my gist with Xcode playground.
A problem that might occur on retina displays is that the scale of the image is set by ImageCapture or so. The resize functions above will not change that. In these cases the resize will work not properly.
In the code below, the scale is set to 1 (not scaled) and the returned image has the size that you would expect. This is done in the
UIGraphicsBeginImageContextWithOptions
call.