How to set the opacity/alpha of a UIImage?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In UIKit, the most important distinction is that UIImage is image data, while UIImageView is the view that displays it. If you only want the image to appear more transparent on screen, set the alpha on the view. If you need a new image asset with baked-in transparency, render a new UIImage with the desired alpha.
Change Display Opacity with UIImageView.alpha
If the goal is visual transparency in the interface, change the view's alpha. This is the simplest and most efficient option because UIKit handles it at display time.
An alpha value of 1.0 is fully opaque and 0.0 is fully transparent. This affects the whole view, including the displayed image content.
This is usually the correct answer when the question is really about how the image looks on screen rather than how to permanently modify the image bytes.
Create a New UIImage with Transparency Applied
If you need a separate image object with a lower opacity, draw the original image into a graphics context and return a new UIImage.
This is the right approach when you want to reuse the transparent version in several places, write it to disk, or pass it to APIs that expect an image rather than a view.
Know When Each Approach Is Better
Use view alpha when:
- the image is only being displayed
- you want animation-friendly transparency
- you do not need a new image object
Use a rendered UIImage when:
- you need the modified image itself
- the result must be cached or exported
- different parts of the app need the same transformed asset
That distinction saves both code and unnecessary rendering work.
Animating Alpha Is Usually a View Concern
If the transparency change is part of a UI animation, animate the UIImageView rather than repeatedly generating new images.
This is much cheaper than recreating image data every frame.
SwiftUI Note
If you are working in SwiftUI rather than UIKit, the equivalent is usually .opacity(...) on the Image view.
That is a view-level change, not a mutation of the underlying bitmap.
Common Pitfalls
- Trying to set an
alphaproperty directly onUIImage.UIImagedoes not have one. - Re-rendering new images when changing
UIImageView.alphawould be enough. - Confusing display transparency with permanent image modification.
- Forgetting that lowering view alpha also affects touch feedback and any overlapping visual composition.
- Generating many modified images inside animations, which can hurt performance.
- Not preserving image scale when rendering a new
UIImage.
Summary
- '
UIImageView.alphais the simplest way to make an image appear transparent on screen.' - '
UIImageitself is immutable image data, so you must render a new image if you want baked-in opacity.' - Use view alpha for presentation and animation.
- Use a new rendered image when you need a reusable modified asset.
- In SwiftUI, the equivalent display technique is
.opacity(...).
Related reading
- How to set the part of the text view is clickable
- How to set the text color of TextView in code?
- How to set the title of a Navigation Bar programmatically?
- How to set the title text color of UIButton?
- How to set the title text color of UIButton?
- How to set the width of a cell in a UITableView in grouped style
- How to set timeout in Retrofit library?
- How to set timeout in Retrofit library?
.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.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.