How to crop UIImage on oval shape or circle shape?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
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.

