David
David

Reputation: 1680

How to load image from Images.xcassets into a UIImage with swift

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!

Upvotes: 39

Views: 40449

Answers (3)

Paweł Brewczynski
Paweł Brewczynski

Reputation: 2743

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)

Upvotes: 0

Waxhaw
Waxhaw

Reputation: 829

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

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

Upvotes: 0

Bright
Bright

Reputation: 5741

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!

Upvotes: 64

Related Questions