UIImage Resize, then Crop
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
When you need an image to fit an exact box, the usual sequence is resize first, then crop. Resizing brings the image close to the target scale while preserving quality, and cropping removes the extra edges. If you skip the order or ignore aspect ratio, the result often looks stretched or blurry.
Why Resize Before Crop
Suppose you want a square avatar from a rectangular photo. If you crop first without thinking about scale, you may throw away useful image detail or work with a much larger bitmap than necessary. A better pattern is:
- scale the image so it fully covers the target size
- crop the centered or chosen region
This is the same idea as “aspect fill” for a bitmap you want to save as a new UIImage.
A Swift Helper Using UIGraphicsImageRenderer
Modern iOS code should prefer UIGraphicsImageRenderer for this kind of work. It handles scale cleanly and is safer than the older image context APIs.
Usage:
This works by scaling the image enough to cover the output box, then drawing it into a renderer whose canvas is already the final size. The extra area falls outside the canvas, which effectively crops it.
Center Crop vs Custom Crop
The previous example performs a center crop. That is usually correct for avatars, thumbnails, and gallery cards. If you need to preserve the top of a portrait photo rather than the center, adjust the drawing origin.
For example, top-aligned crop:
Now the image is still scaled to fill, but the crop keeps the upper content instead of centering vertically.
This is important because “crop” is not only a geometry problem. It is also a content decision.
If You Need Separate Resize and Crop Steps
Sometimes it helps to keep the operations separate for clarity. Here is a two-step version:
This version is useful when your crop rectangle comes from user interaction, such as a drag selection or camera overlay.
Be careful, though: cgImage?.cropping(to:) uses pixel coordinates, not point-based UIKit layout coordinates. If the image has a scale factor, you need to convert correctly.
Image Orientation Matters
A frequent source of bugs is image orientation. Photos coming from the camera can have orientation metadata that makes them display correctly on screen even though the underlying pixel data is rotated.
If cropping seems offset or rotated, normalize the image first:
Then resize or crop the normalized result.
Common Pitfalls
The biggest pitfall is resizing to the exact target dimensions without preserving aspect ratio. That squashes the image instead of cropping it.
Another common issue is mixing points and pixels. UIKit layout works in points, while Core Graphics cropping works on pixel-backed CGImage coordinates.
Orientation problems also show up often with camera images. If the crop region looks wrong, normalize first and test again.
Finally, avoid doing heavy image processing repeatedly on the main thread for large images. Resize and crop work can be expensive, especially in scrolling views or batch imports.
Summary
- For fixed-size output, resize to fill first and crop second.
- '
UIGraphicsImageRendereris a strong modern choice for creating the output image.' - Center crop is common, but the crop origin can be customized.
- Separate resize and crop steps are useful when the crop area comes from user input.
- Watch for aspect ratio, orientation, and point-versus-pixel coordinate issues.
Related reading
- Unable to test and deploy a deeplabv3-mobilenetv2 tensorflow-lite segmentation model for inference
- Understanding COCO evaluation maximum detections
- Understanding Freeman chain codes for OCR
- Understanding super fast blur algorithm
- UIImagePickerController error Snapshotting a view that has not been rendered results in an empty snapshot in iOS 7
- UIImageView - How to get the file name of the image assigned?
- Understanding tf.extract_image_patches for extracting patches from an image
- UnidentifiedImageError cannot identify image file
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.