How can I change the image displayed in a UIImageView programmatically?
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
To change the image in a UIImageView programmatically in Swift, assign a new UIImage to its image property: imageView.image = UIImage(named: "newPhoto"). Images can be loaded from the asset catalog, the app bundle, a file path, remote URLs, SF Symbols, or generated from Data. For animated transitions, wrap the assignment in UIView.transition(with:).
Loading from Asset Catalog
The most common approach — load images added to Assets.xcassets:
UIImage(named:) caches the image in memory. Use UIImage(named:in:compatibleWith:) for images in specific bundles.
Loading from SF Symbols (iOS 13+)
Loading from File Path
UIImage(contentsOfFile:) does not cache the image, making it better for large images that should not stay in memory.
Loading from URL (Async)
Load images from remote URLs using URLSession:
Using async/await (iOS 15+)
Loading from Data
Animated Transitions
Smoothly transition between images using UIView animations:
Available transition options: .transitionCrossDissolve, .transitionFlipFromLeft, .transitionFlipFromRight, .transitionCurlUp, .transitionCurlDown.
Setting Up UIImageView Programmatically
UIImageView in Interface Builder
If the UIImageView is connected via @IBOutlet:
Common Pitfalls
- Updating
imagefrom a background thread: UIKit must be updated on the main thread. SettingimageView.imagefrom aURLSessioncompletion handler withoutDispatchQueue.main.asynccauses crashes or visual glitches. - Using
UIImage(named:)for large images that should not be cached:UIImage(named:)caches images in memory permanently during the app session. For large images (photos, backgrounds), useUIImage(contentsOfFile:)which does not cache and frees memory when the image is deallocated. - Forgetting to set
contentMode: The defaultcontentModeis.scaleToFill, which stretches the image to fill the view, distorting the aspect ratio. Use.scaleAspectFit(fits within bounds) or.scaleAspectFill(fills bounds, may crop) withclipsToBounds = true. - Image name typo returning nil silently:
UIImage(named: "nonexistent")returnsnilwithout an error. The image view shows nothing, and no crash or warning occurs. Double-check asset names match exactly (case-sensitive). - Not handling optional
UIImagefrom network loads: Network requests can fail, return non-image data, or time out. Always useguard letorif letwhen creatingUIImage(data:)and provide a placeholder image for failure cases.
Summary
- Set
imageView.image = UIImage(named: "name")to change the displayed image - Use
UIImage(named:)for asset catalog images (cached) andUIImage(contentsOfFile:)for large files (not cached) - Load remote images with
URLSessionand always updateimageView.imageon the main thread - Use
UIView.transition(with:duration:options:animations:)for smooth image change animations - Set
contentModeto.scaleAspectFitor.scaleAspectFillto maintain proper aspect ratio
Related reading
- How can I change the name of an iOS app in Xcode?
- How can I change the UISearchBar search text color?
- How can I change UIButton title color?
- How can I check for an active Internet connection on iOS or macOS?
- How can I check if a view is visible or not in Android?
- How can I check the system version of Android?
- How can I check what is stored in my Core Data Database?
- How can I check whether dark mode is enabled in iOS/iPadOS?
.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 courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.