How to add a local image to a view in Swift

A step by step tutorial on adding a local image from your assets.xcassets to a view in Swift (iOS). Github repository included.

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 07/20/2022 at 15:52
Last Updated on 08/27/2022 at 12:04
A flower that represents Swift next to a flower that represents XCode. Beneath it sits the text that states 'Add a local image to a UIView'.

A step by step tutorial on adding a local image from your assets.xcassets to a view in Swift (iOS). Github repository included.

The following tutorial builds on our Open Source Swift and demonstrates how to add a local image to a UIImageView. This tutorial starts on the main branch and the changes are available on the tutorials/local-image branch of the repository below.

Clone the repo's main branch, before starting the tutorial.

git clone git@github.com:delasign/swift-starter-project.git
Download Open Source projectHow to create a SwiftLint enabled Swift Xcode Project

Please note this tutorial uses the TinyConstraints Swift package, which we added using the Swift Package Manager.

Discover TinyConstraintsHow to add a Swift Package Manager package dependency

Step One: Declare your variable

A screenshot showing you how to declare your variable in your view.

In CustomUIView.swift, declare your UIImageView and set the image to your local image.

let imageView: UIImageView = UIImageView(image: UIImage(named: SampleImages.placeholder))
How to add and structure local images in Swift

Step Two: Setup your ImageView

A screenshot showing you how to setup your UIImageView.

In CustomUIView+UI.swift setup your image view. Sample code below.

Here we have set the imageView to:

  • Sit below the label, at an offset of 20 pixels.
  • Center itself horizontally on the view
  • Have a width and a height of 250 pixels.

Step Three: Verify

A screenshot of our app showing the image below the label.

Run your app to verify that the image appears.

Any Questions

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.

SubscribeInquireView All PostsView All ServicesView All Work

Partner with us

We would love to get to know you and see how we can help your organization with its goals and needs.
Let's Talk

Stay Informed

Get occasional updates about our company, research, and product launches.
Subscribe