Anand
Anand

Reputation: 1959

Xamarin.forms rename captured image using custom cameraview

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.

Upvotes: 0

Views: 469

Answers (1)

Jarvan Zhang
Jarvan Zhang

Reputation: 1013

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);
}

Upvotes: 1

Related Questions