Problems with getting files created in Swift 2

2

I am saving an image in a directory but when I go to access it returns that the file does not exist and checking the route I find that part of it changes every time I access the attached route example:

/ Users / mac / Library / Developer / CoreSimulator / Devices / B378CE15-4E91-4EA1-9DDD-FCD5DC9D8190 / data / Containers / Data / Application / 21C7E4D9-B138-44C2-80BB-2B431056C0EE /Documents/TMGProfileImage/663565364763465.jpg

/ Users / mac / Library / Developer / CoreSimulator / Devices / B378CE15-4E91-4EA1-9DDD-FCD5DC9D8190 / data / Containers / Data / Application / EEE47491-8D09-40FB-ADBE-78E7B1F83674 /Documents/TMGProfileImage/663565364763465.jpg

What should I do in this case so that the file is not lost?

Thanks

    
asked by Andres Guillermo Castellanos A 13.06.2016 в 21:58
source

2 answers

1

Add the image in the Assets. so Xcode generates a copy in the project directory plus Xcode then you can access that image only by naming it without having to specify format.

    
answered by 13.06.2016 в 22:16
0

Look what happens is that you save the entire route and you NEVER have to do that because the Documents Directory changes. What you have to do is just save the name of your file and concatenate it to the Documents path every time you access it.

For example:

You only save: "TMGProfileImage / 663565364763465.jpg"

and when you want to access the image, concatenate the documentsDirectory with your route:

documentsDirectory () + "TMGProfileImage / 663565364763465.jpg"

func documentsDirectory() -> String {
    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
    let documentsDirectory = paths[0]
    return documentsDirectory
}
    
answered by 12.09.2016 в 19:12