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
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.
- Button (btnStore).
- Label (lblPath)
- 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
- using System;
- using System.IO;
- using Foundation;
- using UIKit;
- namespace XamariniOSImageStore {
- public partial class ViewController: UIViewController {
- protected ViewController(IntPtr handle): base(handle) {
-
- }
- string fileName;
- string folderPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
- int Count = 0;
- public override void ViewDidLoad() {
- base.ViewDidLoad();
-
- }
- partial void UIButton8_TouchUpInside(UIButton sender) {
- StoreImage("Xamarin.jpg");
- }
- public void StoreImage(string filename) {
- if (File.Exists(fileName)) {
- Count++;
- string filenames = "image" + Count.ToString() + ".jpg";
- fileName = Path.Combine(folderPath, filenames);
- } else {
- fileName = Path.Combine(folderPath, "image.jpg");
- }
- var img = UIImage.FromFile(filename);
- NSData image = img.AsJPEG();
- NSError err = null;
- image.Save(fileName, false, out err);
- lblPath.Text = "File Path:" + fileName;
- imgProfile.Image = UIImage.FromFile(fileName);
- }
- public override void DidReceiveMemoryWarning() {
- base.DidReceiveMemoryWarning();
-
- }
- }
- }
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.