Saving and Loading Images, Swift 2.0

Swift 2.0 , Xcode 7.1.1

This will show you how to save and load images in a way that is very easy and straight forward.
Here is the function to save your image first.
We’ll assume you already have an UIImageView, containing an image, named ‘image’ :

Here is how to use the function later in your code:

However before you can Save or Load anything you have to know the ‘path’ you are going to use.
We’ll need 3 things:

      • a function to pinpoint the documents directory
      • a function to pinpoint a file in the documents directory
      • a variable to store the location ( path ) of the image we want to save

Here is how we are going to do that:

Make sure the 2 functions above are placed before the class declaration. For example, before:

At this point you already have the image saved to the documents folder inside your app folder.
Keep in mind that folder will keep changing when using the IOS Simulator.

Here is how to declare the function for loading an image from a Path:

Now we can use that function and load our image and
apply it to an imageView you might have for an example:

Here is how the full example should look:

34 thoughts on “Saving and Loading Images, Swift 2.0

  1. Razvigor Andreev says:

    If you have any question or encounter any errors – please let me know. Cheers!

  2. dancingbush says:

    Hi, Thanks for the tutorial. Will this save to a directory on a real device when running the app?

  3. Razvigor Andreev says:

    Yes, this will save to the default documents folder on your iOS device. If you have a SDK license you can test it, for example with you iPhone plugged in to xCode.

  4. Hey, when I try to return the image from the loadImage fund I get image == nil, directory is /Users/……../Library/Developer/CoreSimulator/Devices/5AB84EDB-424E-4667-8F81-1477FB84E542/data/Containers/Data/Application/928A445B-47E8-4957-AE13-AD913E3B2530/Documents/Case3a.png
    The Application/928A445B-47E8-4957-AE13-AD913E3B2530/ is actually not there….is this expected? You mentioned the destination would change every run. I am saving the URL to core data and then reloading it when app runs again but then the URL does not exist

  5. Razvigor Andreev says:

    Yes, that’s how it is on the simulator. On a real device will stay the same. Return the image within the same run and you’ll see it 😉 So just make 2 UIImageView’s one that will pick up the image and save it and once it’s saved the second UIImageView will auto-load it 😉 This should work fine.

  6. Razvigor Andreev says:

    You don’t have an SDK to try it on a real device ?

    • Sure I’ll try on real device. So why is directory deleted on simulator but not real device? Awkward for testing

      • Razvigor Andreev says:

        It doesn’t get deleted, it’s still there, however on every run the iOS SImulator creates a new folder with it’s unique run-code. Folders are still there, so you should be able to find them in Finder and go there if you copy/paste the location. I don’t think anybody likes this, but since xCode 6 it’s been like that :/

      • Razvigor Andreev says:

        What you can try and do is this: run your app, don’t close the Simulator but press the HOME button, close the app , within the simulator, then reopen it. This should work .

        • dancingbush says:

          tried on a real device same issue: returns nil when image = UIMage(contentsOfFile : path); is called, location on device is…No image loading form path : /var/mobile/Containers/Data/Application/F6A88C03-EBDF-48CB-B9AE-7EBCDC06036F/Documents/Case3a.png

          Log
          Image is stored in this location – can find in Finder: /var/mobile/Containers/Data/Application/F6A88C03-EBDF-48CB-B9AE-7EBCDC06036F/Documents/Case3a.png

  7. dancingbush says:

    The image is saved and returned only in same instance of app running, in between runs although same url a nil is returned….I am taken the saved url form core data and populating a collection view….

  8. dancingbush says:

    Hey,
    So found the issue , this may be useful for others trying to save and retrieve to/from core data with this code. You have to save the imageName (String) ONLY to core data. When saving to docs directory and retrieving form docs directory use documentsDirectory(imageName) – where imageNAme is retrieved/saved form/to core data object. Im guessing the abs path changes every run on simulator and device run form Xcode, so by calling documentsDirectory(imageName) it will find current docs dir on current run, anyway after many hours it works 🙂 Thanks for all your help.

  9. Razvigor Andreev says:

    Oh, yeah, I forgot about the fact that you are using CoreData 🙂 Generally try and save only the imageName as String – yes. Besides anything else, there is no need to save/load the directory path, since it’s always the same 🙂 Thanks for letting me know. Good job !

    Also make sure you install GitHub app on your Mac so you can upload your projects there. That’s the easiest way to get help later one, especially once your project files become very long.

  10. Really useful stuff – thanks!

  11. Jeremy Andrews says:

    Thank you you made it look simple and it worked for my app perfectly

  12. Jeremy Andrews says:

    Thank you you made it look simple and it worked for my app perfectly

  13. Jeremy Andrews says:

    How do I acknowledge your code that I have used

  14. Jeremy Andrews says:

    How do I acknowledge your code that I have used

Leave a Reply

Your email address will not be published. Required fields are marked *