How to crop UIImage on oval shape or circle shape?
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
Cropping a UIImage into a circle or oval is common for avatars, profile previews, and cards. The effect looks simple, but good results depend on choosing the right target size, handling rectangular source images correctly, and deciding whether you need a real cropped bitmap or only a masked display.
Start With a Square for Circle Crops
A true circle needs a square drawing area. If the source image is rectangular, crop a centered square first.
Without this step, a circular mask applied to a rectangular image often leaves the subject stretched or off-center.
Render the Circular Image
Once the image is square, draw it into an oval clipping path with UIGraphicsImageRenderer.
UIGraphicsImageRenderer is the modern UIKit drawing API and generally behaves better than older context APIs for scale handling.
Create an Oval Crop
An oval crop uses the same clipping approach, but the target width and height differ.
This is useful for wide avatar chips, story cards, or other designs where a full circle is not the intended shape.
Use the Result in UIKit
A common UIKit flow is to preprocess the image once and assign the result to an image view.
Preprocessing can be better than view-only masking when the same shaped asset is reused often.
SwiftUI Display Masking Versus Real Cropping
In SwiftUI, you can either preprocess a UIImage or clip only at display time.
If you only need the on-screen visual effect, clipShape(Circle()) is simpler. If you need a real image for upload, caching, or export, create a new bitmap instead.
Performance Considerations
Image rendering can become expensive if repeated during scrolling or cell reuse. A few practical rules help:
- preprocess once when possible
- cache by image identifier and output size
- avoid re-rendering the same crop during every redraw
The code is straightforward, but repeated rendering can still show up as dropped frames on older devices.
Common Pitfalls
The biggest mistake is applying a circular mask to a non-square source image and expecting a correct avatar crop automatically.
Another common issue is confusing display clipping with real bitmap cropping. A clipped view does not automatically produce a new image file. Developers also often re-render the same crop repeatedly inside performance-sensitive views instead of caching it.
Summary
- Circle crops work best by center-cropping to a square first.
- Use
UIGraphicsImageRendererfor modern image rendering. - Oval crops use the same approach with a non-square target size.
- Preprocess the bitmap when you need reuse, caching, or export.
- Use view-level clipping only when the effect is purely visual.
Related reading
- How to debug Tensorflow segmentation fault in model.fit?
- How to detect how far the object on photo is from another objects on that photo?
- How to detect rotated object from image using OpenCV?
- How to do multi-class image classification in keras?
- How to customize the background color of a UITableViewCell?
- How to customize the background/border colors of a grouped table view cell?
- How to extract and recognize the vehicle plate number with Python?
- How to find and crop words into individual images with Python OpenCV?
.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.