0

In my xamarin.forms app, I have a custom camera view with circle overlay. I can able to take picture in both android and iOS.The difficulty I am facing is I need to rename the taken image into some specific format which contain the picture taken date time. But I am unable to rename the taken picture on both android and iOS.

In iOS,inside my native cameraview, I can get the captured image like this.

var videoConnection = stillImageOutput.ConnectionFromMediaType(AVMediaType.Video);
                var sampleBuffer = await stillImageOutput.CaptureStillImageTaskAsync(videoConnection);
                var jpegData = AVCaptureStillImageOutput.JpegStillToNSData(sampleBuffer);
                var photo = new UIImage(jpegData);

The photo contains captured image on iOS.

How can I rename this image? Any help is appreciated.

Anand
  • 1,546
  • 2
  • 14
  • 32
  • what do you mean by "rename"? Do you mean the file, or some metadata in the file – Jason Sep 24 '20 at 15:09
  • @Jason Yes the image. Currently I am trying to send to my web service using Multipartform data. I need the image name to be some format..like 19072020abcd.jpeg – Anand Sep 24 '20 at 15:12
  • just save the UIImage to a file and name it whatever you want – Jason Sep 24 '20 at 15:15
  • @Jason You mean save to local storage? – Anand Sep 24 '20 at 15:16
  • Yes. If you don't save it then it doesn't have a name. – Jason Sep 24 '20 at 15:18
  • @Jason Oh. Sorry for not detailing my portion. What I am trying to do is when we capture the image, I am trying to send the taken image to web service. Which I not Implemented yet. So what will be the name when we send the photo using multipart formdata without storing to local storage?It is not possible? – Anand Sep 24 '20 at 15:21
  • 1
    When you upload the file you can specify any arbitrary file name. The question you actually asked doesn't seem to have any relation to the problem you are actually trying to solve. – Jason Sep 24 '20 at 15:25
  • @Jason Can you guide me how to give arbitary file name to the photo? – Anand Sep 24 '20 at 15:28
  • https://stackoverflow.com/questions/19954287/how-to-upload-file-to-server-with-http-post-multipart-form-data – Jason Sep 24 '20 at 15:30
  • @Jason Sir, Just another doubt. Ignore if blunder. What I my final goal is when we upload image to db, I want the image name to be in my desired format. Thats what I am trying to do.. So when we take picture, does it have any property named "Name" or something? I thought we can rename that portion. I actually no clarification on this area – Anand Sep 25 '20 at 04:55
  • Try to create a name-string and change the string when taking the picture. Upload both the image file and the name-string to the database, you could get the image file according to the string value. – Jarvan Zhang - MSFT Sep 25 '20 at 06:09
  • @JarvanZhang-MSFT Thanks .Currently the db only have field for Image. Can we not rename the image file itself? Can we store the image and rename. Then clear it from storage. Will this way work? – Anand Sep 25 '20 at 06:12
  • Yes, you could store the file local to rename the image and delete the file from storage. Add a string filed to the table is an alternative method. – Jarvan Zhang - MSFT Sep 25 '20 at 06:30
  • @JarvanZhang-MSFT Can you guide me on the first part? rename a saved file and delete? I have no idea on iOS native part. Any help on that part is appreciated.. the second method is not possible for me because DB is not accessible for me – Anand Sep 25 '20 at 06:34
  • I post the related code, you could refer to it. – Jarvan Zhang - MSFT Sep 25 '20 at 06:57

1 Answers1

1

To store the file locally and then delete the file, try the following steps:

First,try getting the file stream from the UIImage.

var img = new UIImage();
NSData data = img.AsPNG();
Stream output;
data.AsStream().CopyTo(output);

Then save the file locally with the stream.

var documents_path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
documents_path  = Path.Combine(documents_path, "temp");
Directory.CreateDirectory(documents_path);

string filePath = Path.Combine(documents_path, name);
byte[] bytes = new byte[output.Length];
using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate))
{
    using (output)
    {
        output.Read(bytes, 0, (int)output.Length);
    }
    int length = bytes.Length;
    fs.Write(bArray, 0, length);
}

Delete the file with the file path.

if (File.Exists(filePath))
{
    File.Delete(filePath);
}
Jarvan Zhang - MSFT
  • 898
  • 2
  • 6
  • 51
  • let me check Jarvan – Anand Sep 25 '20 at 07:03
  • Hi sorry for not updating. It saves the file to storage with my desired name. Thanks. One more doubt, Can I send the "fs" ie; filestream as multipart form data? – Anand Sep 25 '20 at 10:23