39

I have an image from the Images.xcassets folder and I want to load it into a UIImage programmatically using swift. How can I do this? Anyone done this before? Please give me some code examples!

1
  • 3
    let img : UIImage = UIImage(named: "here is the name of the image from Images.xcassets"); Commented Aug 22, 2015 at 5:02

3 Answers 3

64

Here's my code:

@IBOutlet weak var imageView: UIImageView! override func viewDidLoad() { super.viewDidLoad() imageView.image = UIImage(named: "Apple") } 

I created an image view and displayed an image inside it.

Enjoy coding!

Sign up to request clarification or add additional context in comments.

Comments

0

You can also create a 'standard' color xcassets item and load it via:

UIColor(named: "--asset--name--here--") 

Comments

0

With Xcode 16 the .xcassets identifiers are autogenerated so you can:

UIImage(resource: .logo) 

Assuming that you have Image Set named logo in default bundle's Assets.xcassets

(Not exactly sure how it works internally as if you "jump to definition" of .logo you'll end up in Assets.xcassets with selected logo image set)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.