Programmatically set image to UIImageView with Xcode 6.1/Swift
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Assigning an image to a UIImageView is simple in UIKit, but blank images usually come from setup problems rather than from the assignment itself. The main jobs are loading a valid UIImage, putting the image view into the hierarchy, and giving it layout and content-mode settings that make the image visible.
Set an Image from the Asset Catalog
If the image is bundled with the app, the most direct approach is to load it by name and assign it to the image property.
UIImage(named:) returns an optional. If the asset name is misspelled or missing from the bundle, the result is nil and the view shows nothing.
Assign an Image to an Outlet-Based Image View
If the image view already exists in a storyboard or nib, connect an outlet and assign the image after the view has loaded.
This is the same operation with less view construction code. The main difference is that storyboard layout already provides the frame and constraints.
Loading an Image from Disk or Data
When the image is not in the app bundle, create the UIImage from data instead of using an asset name.
This is common for user-generated photos, cached downloads, or edited images saved locally by the app.
Load Remote Images Without Blocking the UI
Network image loading should happen asynchronously. UIKit updates still belong on the main thread.
This keeps the interface responsive while the download happens.
Make the Image View Actually Display Correctly
The image assignment itself is only half the story. contentMode controls whether the image fits, fills, or centers inside the view. Constraints determine whether the image view has a real size. Background color can help during debugging because it shows whether the image view exists even when the image is missing.
A very common debugging step is to set a visible frame or obvious constraints and give the image view a temporary background color. If the colored box appears but the image does not, the problem is image loading. If the box does not appear, the problem is layout.
Common Pitfalls
The most common issue is a wrong asset name. UIImage(named:) fails quietly by returning nil.
Developers also forget that the image view needs size. A correct image assignment still shows nothing if the view has zero width or height.
Loading remote data on the main thread causes UI stalls. Fetch asynchronously and update the image view on the main thread.
Summary
- Set an image programmatically by assigning a
UIImagetoimageView.image. - Use
UIImage(named:)for bundled assets andUIImage(data:)for file or network data. - Make sure the image view is in the view hierarchy and has real layout constraints.
- Check
contentMode, asset name, and thread usage when the image does not appear.
Related reading
- Programmatically set left drawable in a TextView
- Programmatically set the initial view controller using Storyboards
- Programmatically set the initial view controller using Storyboards
- Programmatically switching between tabs within Swift
- Programming with SurfaceView and thread strategy for game development
- ProgressDialog is deprecated.What is the alternate one to use?
- Proper practice for subclassing UIView?
- Proper Realm usage patterns/best practices?
.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.