Search code examples
iosswift3

Swift 3 : How to get path of file saved in Documents folder


path = Bundle.main.path(forResource: "Owl.jpg", ofType: "jpg")

returns nil, however, using NSHomeDirectory() I'm able to verify that is under Documents/ folder.


Solution

  • First, separate name and extension:

    Bundle.main.path(forResource: "Owl", ofType: "jpg")
    

    Second, separate (mentally) your bundle and the Documents folder. They are two completely different things. If this file is the Documents folder, it absolutely is not in your main bundle! You probably want something like this:

    let fm = FileManager.default
    let docsurl = try! fm.url(for:.documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
    let myurl = docsurl.appendingPathComponent("Owl.jpg")
    

    Third, if Owl is an image asset in the asset catalog, then say

    let im = UIImage(named:"Owl") // or whatever its name is