Adrian
Adrian

Reputation: 20068

How to programmatically load an .pdf image from the Images.xcassets

I have tried this:

self.relationshipImage.image = UIImage(named: "pic1")

self.relationshipImage.image = UIImage(named: "pic2", inBundle: NSBundle.mainBundle(), compatibleWithTraitCollection: nil)

Where pic1 and pic2 are .pdf files. I can select them in the storyboard, but not programmatically.

Any idea how to do this ?

Upvotes: 2

Views: 1503

Answers (2)

Nishant
Nishant

Reputation: 12617

Depending on where your asset catalog is being stored, you may have to explicitly point to its enclosing bundle. You try this code:

let bundle = NSBundle(forClass: self.classForCoder)
let image = UIImage(named: "pic1", inBundle: bundle, compatibleWithTraitCollection: nil) 

This will look for the image in the same bundle as the class where you are requesting the image.

Upvotes: 4

Jason Nam
Jason Nam

Reputation: 2011

You have to refer to the image with this.

enter image description here

Upvotes: 0

Related Questions