How can I change the image displayed in a UIImageView programmatically?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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 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 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?
.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.