CGContextDrawImage draws image upside down when passed UIImage.CGImage
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If CGContextDrawImage draws a UIImage.CGImage upside down, the usual cause is a coordinate-system mismatch. Core Graphics draws with an origin at the bottom-left by default, while UIKit works with an origin at the top-left, so a UIImage that looks correct in UIKit can appear flipped when drawn directly through Core Graphics.
Why the image flips
UIImage is a UIKit type. CGImage is a lower-level Core Graphics representation. When you pass image.CGImage into CGContextDrawImage, Core Graphics uses its own coordinate assumptions, not UIKit’s.
That means code like this often produces an upside-down image:
The image data itself is not necessarily wrong. The problem is how the context interprets the coordinate system.
Fix it by flipping the context
The standard fix is to translate and scale the context before drawing.
The vertical scale of -1 flips the drawing space, and the translation moves the origin so the image lands in the expected place.
Use UIImage.draw(in:) when you want UIKit behavior
If you are already in UIKit drawing code and do not need low-level Core Graphics behavior, the simpler option is often to let UIImage draw itself.
UIImage.draw(in:) respects UIKit’s coordinate conventions and also handles UIImage orientation metadata more naturally.
Watch out for image orientation
Another wrinkle is that UIImage can carry orientation information that CGImage does not preserve in the same high-level way. If the image came from the camera or photo library, image.imageOrientation may not be .up.
If you draw the raw CGImage, you may need to normalize orientation first.
This does not replace the coordinate flip in every case, but it solves a related class of orientation surprises.
When direct Core Graphics drawing still makes sense
You may still want CGContextDrawImage when:
- composing into a bitmap context
- applying low-level transforms
- mixing vector and bitmap drawing manually
- generating offscreen images
In those cases, it is worth understanding the coordinate system rather than relying on higher-level UIKit drawing helpers.
A complete bitmap-context example
This creates a new image with the expected visual orientation in UIKit.
Common Pitfalls
The biggest mistake is assuming UIImage and CGImage share the same drawing conventions. They do not. UIKit and Core Graphics use different coordinate assumptions.
Another issue is applying the flip transform but forgetting to restore the graphics state. That can cause later drawing operations to inherit the flipped coordinate system unexpectedly.
Developers also confuse vertical coordinate flipping with UIImage orientation metadata. These are related but separate concerns. A camera image can have orientation issues even after the context itself is corrected.
Finally, if you only need ordinary UIKit drawing, do not force everything through CGContextDrawImage. UIImage.draw(in:) is often the simpler and safer API.
Summary
- '
CGContextDrawImageuses Core Graphics coordinates, which differ from UIKit coordinates.' - A
UIImage.CGImageoften appears upside down unless you flip the context first. - Use
translateByandscaleByto correct the drawing space. - '
UIImage.draw(in:)is simpler when you want UIKit-friendly behavior.' - Image-orientation metadata can create additional issues beyond the basic upside-down rendering problem.

