Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Figuring assets.xcassets path programmatically using SWIFT

Trying to figure the path to the images with swift code. I got this that I think works in objective C.

[UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.png", [[NSBundle mainBundle] resourcePath], imgName]];

To get the path for my .png image I stored in assets.xcassets. But I need to do so in SWIFT now, not objective C.

I need the path cause I am trying to upload the image I put there to an CKAsset in iCloud.

like image 816
user3069232 Avatar asked Dec 23 '15 21:12

user3069232


1 Answers

If you only need the path to the file (and not an instance of UIImage), this will do it:

Swift 2:

if let resourcePath = NSBundle.mainBundle().resourcePath {
    let imgName = "dog.png"
    let path = resourcePath + "/" + imgName
}

Swift 3/4:

if let resourcePath = Bundle.main.resourcePath {
    let imgName = "dog.png"
    let path = resourcePath + "/" + imgName
}
like image 133
James Zaghini Avatar answered Nov 09 '22 03:11

James Zaghini