Store Images In App Directory In Xamarin iOS

Introduction

Xamarin is a platform to develop cross-platform and multi-platform apps (for example, Windows Phone, Android, iOS). In Xamarin platform, the code sharing concept is used. In Xamarin Studio, Visual Studio is also available.
 

Prerequisites
  • Xamarin Studio.
  • Xcode.
The steps given below are required to be followed in order to store images in App Directory in Xamarin iOS.

Step 1

Go to Xamarin Studio.

Click New Solution—> select iOS—>select App--> choose Single View app. Afterwards, click Next.

 

Step 2

In this step, configure your app. Give the app name (Ex: sample) and Organization Identifier. Afterwards, click Next.

 

Step 3

In this step, give your project name (Ex: Sample) and solution name (Ex: Sample). Give the path to your project. Afterwards, click "Create".

 

Step 4

Subsequently, go to the solution. In there, you get all the files and sources of your project. Now, select Main.storyboard and double-click to open the Main.storyboard page.

 

Step 5

After opening the Main.storyboard, you can design this page, as per your desire.

 

Step 6

In this step, design your app.using Storyboard, Toolbox, and Properties.
  1. Button (btnStore).
  2. Label (lblPath)
  3. ImageView (imgProfile).
 

Step 7

In this step, and add the image form your local system. Go to Solution—>Resource-->right click and Add-->Add Files. Now, you can choose the required image. Click "Open".

Next, choose to copy the files to the directory. Afterwards, click OK. (Or) Pick an image from your device. Refer this link.

 

Step 8

Go to the ViewController.cs page and write the code given below.

ViewController.cs
  1. using System;  
  2. using System.IO;  
  3. using Foundation;  
  4. using UIKit;  
  5. namespace XamariniOSImageStore {  
  6.     public partial class ViewController: UIViewController {  
  7.         protected ViewController(IntPtr handle): base(handle) {  
  8.             // Note: this .ctor should not contain any initialization logic.  
  9.         }  
  10.         string fileName;  
  11.         string folderPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);  
  12.         int Count = 0;  
  13.         public override void ViewDidLoad() {  
  14.             base.ViewDidLoad();  
  15.             // Perform any additional setup after loading the view, typically from a nib.  
  16.         }  
  17.         partial void UIButton8_TouchUpInside(UIButton sender) {  
  18.             StoreImage("Xamarin.jpg");  
  19.         }  
  20.         public void StoreImage(string filename) {  
  21.             if (File.Exists(fileName)) {  
  22.                 Count++;  
  23.                 string filenames = "image" + Count.ToString() + ".jpg";  
  24.                 fileName = Path.Combine(folderPath, filenames);  
  25.             } else {  
  26.                 fileName = Path.Combine(folderPath, "image.jpg");  
  27.             }  
  28.             var img = UIImage.FromFile(filename);  
  29.             NSData image = img.AsJPEG();  
  30.             NSError err = null;  
  31.             image.Save(fileName, false, out err);  
  32.             lblPath.Text = "File Path:" + fileName;  
  33.             imgProfile.Image = UIImage.FromFile(fileName);  
  34.         }  
  35.         public override void DidReceiveMemoryWarning() {  
  36.             base.DidReceiveMemoryWarning();  
  37.             // Release any cached data, images, etc that aren't in use.  
  38.         }  
  39.     }  
  40. }  
 

Step 9

Now, go to "Run" option, choose Debug, and from the list of available iPhone and iPad simulators, choose any one to run it.

 

Output

After a few seconds, the app will start running on your iPhone simulator. You will see your app working successfully.

Go to check your App Directory. There is no image.

(Ex:/Users/DELPIN/Library/Developer/CoreSimulator/Devices/48FC377D-BA0C-40F8-9545-BA9D7DE5EFE9/data/Containers/Data/Application/258B5DED-15D1-4048-B54B-0D21B945DFDA/Documents)

 

Just click the "Store" button, The image will be stored successfully.

 

After this, check your App Directory again. You will find the images there.

Ex:/Users/DELPIN/Library/Developer/CoreSimulator/Devices/48FC377D-BA0C-40F8-9545-BA9D7DE5EFE9/data/Containers/Data/Application/258B5DED-15D1-4048-B54B-0D21B945DFDA/Documents) 

 

Summary

This was the process of storing images in the App Directory in Xamarin iOS. I hope you find it helpful. Please dhare your comments and feedback.

Up Next
    Ebook Download
    View all
    Learn
    View all