0

I need to save the image in custom folder location in iOS. I have used the following code to save the image while is saved in default location in devices.

        UIGraphics.BeginImageContext(nativechart.Frame.Size);
        nativechart.Layer.RenderInContext(UIGraphics.GetCurrentContext());

        UIImage image = UIGraphics.GetImageFromCurrentImageContext();
        UIGraphics.EndImageContext();

        //Captured chart image will save into photo album

        image.SaveToPhotosAlbum(new UIImage.SaveStatus(delegate (UIImage img, NSError error) { }));

        ////save image

        NSData data = image.AsJPEG(1.0f);
        NSFileManager fileManager = NSFileManager.DefaultManager;
        string[] paths = NSSearchPath.GetDirectories(NSSearchPathDirectory.PicturesDirectory, NSSearchPathDomain.All, true);
        string documentsDirectory = paths[0];
        string fullPath = documentsDirectory + "/" + filename;
        fileManager.CreateFile(fullPath, data, new NSDictionary());

Could you please help me out to save the image in custom folder location?

Parthiban
  • 168
  • 1
  • 13
  • Look at these examples https://stackoverflow.com/questions/32836862/how-to-use-writetofile-to-save-image-in-document-directory https://stackoverflow.com/questions/44646186/save-file-in-document-directory-in-swift-3 https://stackoverflow.com/questions/43016666/how-to-save-a-uiimage-to-documents-directory – Gagan_iOS Sep 19 '17 at 09:18
  • @Gagan_iOS Thanks for the update. Can you help me out in Xamarin iOS (ie) C# code. – Parthiban Sep 19 '17 at 09:23
  • Look in my answer with code. – Gagan_iOS Sep 19 '17 at 09:25

1 Answers1

4

Below is the code for saving image in document directory

   var photo = yourImageFile as UIImage;
   var documentsDirectory = Environment.GetFolderPath
                         (Environment.SpecialFolder.Personal);
    var directoryname = Path.Combine(documentsDirectory, "FolderName");
   Directory.CreateDirectory(directoryname);
   string jpgFilename = System.IO.Path.Combine (directoryname, "Photo.jpg"); // hardcoded filename, overwritten each time. You can make it dynamic as per your requirement.

   NSData imgData = photo.AsJPEG();
   NSError err = null;
   if (imgData.Save(jpgFilename, false, out err)) {
       Console.WriteLine("saved as " + jpgFilename);
   } else {
       Console.WriteLine("NOT saved as " + jpgFilename + " because" + err.LocalizedDescription);
   }

Source Link

Gagan_iOS
  • 3,638
  • 3
  • 32
  • 51
  • The above code has saved the image in default location. I need to save the image in custom folder. – Parthiban Sep 19 '17 at 09:34
  • yes.. add a custom path for that. Just we do in iOS. Your custom folder will be available inside document directory. – Gagan_iOS Sep 19 '17 at 09:36
  • Sorry, Can you please guide where need to the custom path for that ? – Parthiban Sep 19 '17 at 09:38
  • At this link for iOS only https://forums.xamarin.com/discussion/94306/saving-image-to-local-folder iOS-SAVE File var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); var directoryname = Path.Combine(documents, "FolderName"); Directory.CreateDirectory(directoryname); FilePath = Path.Combine(directoryname, fileName) //You can set the fileName what you want just take care of extension iOS-GET File var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); var directoryname = Path.Combine(documents, "FolderName" + "/" + fileName); – Gagan_iOS Sep 19 '17 at 09:39