Clicky

iOS Dev Nugget 110 Resizing and Cropping an Image

.

Need to run a code review on your codebase? Hire me

Sometimes given a photo, you want to (1) generate a smaller version of the photo and (2) crop it so it has a specific dimension, preserving it's aspect ratio and display the photo1. The code to do this separately isn't difficult and there are many versions of it available online.

So you can either do:

(1) Crop then (2) Resize

or

(2) Resize then (1) Crop

Intuitively, it might seem there isn't much of a difference, but if you factor in the fact that iPhones (as of iPhone 6 Plus) can shoot panoramas as big as 43 megapixels or ~11-12 MB, choosing the right approach reduces the chance of crashing due to running out of memory.

With some profiling, you'll find that generally, it's better to do:

(2) Resize then (1) Crop

[1] You can't always do a resize, skip the crop, utilizing contentMode=contentUIViewContentModeScaleAspectFill because you might want to process the result, say with a mask.


Your feedback is valuable: Do you want more nuggets like this?   Yes   or   No

.

Like this and want such iOS dev nuggets to be emailed to you, weekly?

Sign Me Up! or follow @iosdevnuggets on Twitter

.

View archives of past issues