UIImage
opacity
alpha
iOS development
Swift programming

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.

Browse interview questions

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.

swift
1import UIKit
2
3let imageView = UIImageView(image: UIImage(named: "avatar"))
4imageView.alpha = 0.4

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.

swift
1import UIKit
2
3extension UIImage {
4    func applyingAlpha(_ alpha: CGFloat) -> UIImage {
5        let format = UIGraphicsImageRendererFormat.default()
6        format.scale = scale
7
8        let renderer = UIGraphicsImageRenderer(size: size, format: format)
9        return renderer.image { _ in
10            draw(in: CGRect(origin: .zero, size: size), blendMode: .normal, alpha: alpha)
11        }
12    }
13}
14
15let original = UIImage(systemName: "star.fill")!
16let faded = original.applyingAlpha(0.3)

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.

swift
UIView.animate(withDuration: 0.25) {
    imageView.alpha = 0.2
}

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.

swift
1import SwiftUI
2
3struct ContentView: View {
4    var body: some View {
5        Image(systemName: "star.fill")
6            .opacity(0.3)
7    }
8}

That is a view-level change, not a mutation of the underlying bitmap.

Common Pitfalls

  • Trying to set an alpha property directly on UIImage. UIImage does not have one.
  • Re-rendering new images when changing UIImageView.alpha would 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.alpha is the simplest way to make an image appear transparent on screen.'
  • 'UIImage itself 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.