1

Share

APP DEVELOPMENT

How To Store An Image From Document In Swift?

  • Home
  • Blogs
  • How To Store An Image From Document In Swift?

A document directory is in the application container, which is used by the system to save (documents, images, reports, and other) files safely.

Somish Kakadiya

June 23, 2022

Step - 1: Introduction

Document directory is used for saved data in device memory. Save and get images from the document directory is easy. We need only the document directory path for getting images from the document directory. After getting the image we use the image easily.

Step - 2: Open Xcode

 

 

Open Xcode and create a new single view app. For product name, use DocumentDirectoryDemo and then fill out the organization name and organization identifier with your customary values. Enter Swift as Language and choose Next.

Step - 3: Add Any Image In Project

First, we start to save images in the document we need one image to save in the document directory. So we can choose an image from the device’s gallery or add an image manually in our project. First of all, we add manually image in the project asset.

Here, I add an image in the asset of the project and use this image to save in the document directory.

 

 

Step - 4: Open Main Storyboard

We need an ImageView to show image when we get it from the document directory. So open the main storyboard and search image view in the tool.

 

 

Now select image view and drag nd drop in view controller.

 

 

Step - 5: Create Outlet

Now open view controller class file and create an outlet of the image view.

Step - 6: Called Function

After creating a function called this function in ViewDidload. So every time we run the project to check if the folder does not exist then create a folder in the document directory otherwise continue to save the image in an existing folder.

After called the function in ViewDidload run the project.

Here we get one path of our folder in the document directory.

After the run project, we get the folder path. Copy this path and find in finder we get our folder.

// Output :-

 

 

Step - 7: Save Image In Document Directory

First, we create a function for getting the document directory path for save and get image.

This path is useful for when we save and get images in the document directory.

Step - 8: Return Directory Path As Url

This function returns the directory path as a URL.

We use this URL for saving and get images in the document directory.

Now create a function to save images.

Here my image name is “14” and I want to save this image in the document directory with the name “MyImage.png”.

Step - 9: Call Function

Call this function in view did load.

After call function SaveImagDocumentDirectory in ViewDidLoad run project.

After run the project open folder in the directory, we found a new saved image.

 

 

Now we saved image successfully in document directory.

Step - 10: Get Image From Document Directory

Now we get this image from the document directory.

Here my image view outlet name is ImgViewOutlet.

Now we create a function to get images from the folder of the document directory.

Step - 11 : ViewDidLoad

After create function call this function in ViewDidLoad.

Step - 12: Run Project

After call function in ViewDidLoad run project.

Select device or any simulator press play button and run project.

 

 

Step - 13: Output

After the run project, we get our final output.

Image show in ImageView. This image gets from the document directory.

Final output image attached.

 

 

 

Conclusion

We need a document directory to safely store our files. Here, we have created a step-by-step guide with an example. It will make you understand the process clearly and will be helpful in developing it for your business.

img

Somish Kakadiya

CTO of Vasundhara Infotech, a leading Software development company in the USA. His technological interests has helped the company in making useful decisions.

message

Have a project in mind? Drop a message to Bansi Pipaliya & start the discussion!

Get a Newsletter

Sign Up to our newsletter to get latest updates staight in your inbox.

Vasundhara respects your privancy. No Spam!

Get a Newsletter

Sign Up to our newsletter to get latest updates staight in your inbox.

Vasundhara respects your privancy. No Spam!

message

Have a project in mind? Drop a message to Bansi Pipaliya & start the discussion!

Latest 13 Web Development Trends To Expect In 2022
April 11, 2022 Category : company news

Revealing Vasundhara’s New Identity

Read More
Latest 13 Web Development Trends To Expect In 2022
May 17, 2022 Category : web development

Latest 13 Web Development Trends To Expect In 2022

Read More
Leave a Comment