How to rotate image in Swift?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Rotating an image in Swift can mean two different things: rotating how an image view is displayed, or creating a new UIImage whose pixels are actually redrawn at a new angle. Those are different operations. Display-only rotation is cheap and good for UI effects, while pixel rotation is the right choice when you need to save, upload, or further process the rotated image.
Rotate the View for Display Only
If you only want the image to appear rotated on screen, transform the UIImageView instead of creating a new image.
This is the simplest approach for interface animation or temporary display changes. The underlying UIImage is not modified. Only the view’s presentation changes.
That means if you later read imageView.image, you still get the original image data.
Create a New Rotated UIImage
If you need the rotated result as a real image, redraw it into a graphics context.
This produces a new UIImage with pixels rendered into a rotated canvas.
Preserve the Whole Image Bounds
The tricky part of image rotation is canvas size. If you reuse the original width and height blindly, the corners may be clipped after rotation. That is why the example computes newSize by applying the rotation transform to the original rectangle first.
In practical terms:
- view transforms rotate presentation only
- image redrawing needs a large enough destination canvas
If the result looks cropped, the destination size is usually the problem.
Rotate by Fixed Right Angles
For common cases such as ninety-degree rotation, using explicit helpers can make the code clearer.
That makes intent obvious at the call site and avoids repeated angle literals.
Orientation Metadata vs Pixel Rotation
Some images appear “rotated” because of orientation metadata rather than because the bitmap is already in the wrong direction. This is common with photos captured from the camera.
In that case, the image may display correctly in some contexts and incorrectly in others depending on whether the consumer respects the orientation flag. If you need a normalized image with upright pixels, redraw it into a new image context rather than only trusting metadata.
A simple normalization helper looks like this:
This does not arbitrarily rotate by a custom angle. It flattens the current orientation into actual pixel data.
Choose the Right Tool for the Job
Use view transforms when:
- the rotation is only for display
- you want animation
- you do not need a new saved image
Use image redrawing when:
- you need to export the rotated image
- you need to chain image-processing operations
- you want a stable pixel representation independent of orientation metadata
Choosing the lighter operation often makes the code simpler and faster.
Common Pitfalls
The most common mistake is rotating the UIImageView and expecting the underlying UIImage data to be permanently changed. Another is redrawing into a context with the original image size and then being surprised by cropped corners. Developers also often confuse metadata-based orientation issues with actual pixel rotation, which leads to applying the wrong fix. A final issue is repeatedly rotating already-rotated images, which can degrade quality if the image is redrawn many times.
Summary
- Rotating an image view is different from creating a rotated image.
- Use a view transform for display-only rotation.
- Redraw the image into a graphics context when you need a new rotated
UIImage. - Compute a destination canvas large enough to avoid clipping.
- Distinguish between pixel rotation and orientation normalization before choosing an approach.

