Take/Choose and save picture using UIImagePickerControllerDelegate

christopherpolskachristopherpolska PLUniversity ✭✭✭
edited February 2017 in Cross Platform with Xamarin

Hi :)

I'm developing a simple app with MvvmCross to take pictures (take with camera or choose from album).
In my Model in the PCL project I keep just the name of the picture - the picture is located in application's folder called "Resources".

On the button click event I call the below code (on iOS project):

public class MyCamera: IMyCamera
{
private byte[] Bytes;
private string imageName;

public async Task<string> ChoosePicture()
{
    var tcs = new TaskCompletionSource<string>();
    CameraImplementation.SelectPicture(
    UIApplication.SharedApplication.KeyWindow.RootViewController,
    (imagePickerResult) =>
    {
           tcs.TrySetResult(imageName);
    });
    return imageName; 
}

(...)`

CameraImplementation implementation:

` public static class CameraImplementation
{
static UIImagePickerController picker;
static Action _callback;
private static UIImageView newImage;

    static void Init()
    {
        if (picker != null)
            return;

        picker = new UIImagePickerController();
        picker.Delegate = new CameraDelegate();
    }

    class CameraDelegate : UIImagePickerControllerDelegate
    {
        public override void FinishedPickingMedia(UIImagePickerController picker, NSDictionary info)
        {
            var cb = _callback;
            _callback = null;

            picker.DismissViewController(true, (Action)null);
            cb(info);

            //newImage = info.;
            foreach(var i in info)
            {
                var k = i.ToString();
            }

        }

        public override void Canceled(UIImagePickerController picker)
        {
            var cb = _callback;
            _callback = null;

            picker.DismissViewController(true, (Action)null);
            cb(null);
        }
    }

    public static void TakePicture(UIViewController parent, Action<NSDictionary> callback)
    {
        Init();
        picker.SourceType = UIImagePickerControllerSourceType.Camera;
        _callback = callback;
        parent.PresentViewController((UIViewController)picker, true, (Action)null);
    }

    public static void SelectPicture(UIViewController parent, Action<NSDictionary> callback)
    {
        Init();
        picker.SourceType = UIImagePickerControllerSourceType.PhotoLibrary;
        _callback = callback;
        parent.PresentViewController((UIViewController)picker, true, (Action)null);
    }
}

`

It all works fine, the problem is that I do not know how to save the picture to the given folder and rename the file to the name I want.

The screen shows what I have in the NSDictionary info:

Answers

  • assegdassegd Member ✭✭

    @christopherpolska said:
    Hi :)

    I'm developing a simple app with MvvmCross to take pictures (take with camera or choose from album).
    In my Model in the PCL project I keep just the name of the picture - the picture is located in application's folder called "Resources".

    On the button click event I call the below code (on iOS project):

    public class MyCamera: IMyCamera
    {
    private byte[] Bytes;
    private string imageName;

    public async Task ChoosePicture()
    {
    var tcs = new TaskCompletionSource();
    CameraImplementation.SelectPicture(
    UIApplication.SharedApplication.KeyWindow.RootViewController,
    (imagePickerResult) =>
    {
    tcs.TrySetResult(imageName);
    });
    return imageName;
    }
    (...)`

    CameraImplementation implementation:

    ` public static class CameraImplementation
    {
    static UIImagePickerController picker;
    static Action _callback;
    private static UIImageView newImage;

    static void Init()
    {
    if (picker != null)
    return;

    picker = new UIImagePickerController();
    picker.Delegate = new CameraDelegate();
    }

    class CameraDelegate : UIImagePickerControllerDelegate
    {
    public override void FinishedPickingMedia(UIImagePickerController picker, NSDictionary info)
    {
    var cb = _callback;
    _callback = null;

              picker.DismissViewController(true, (Action)null);
              cb(info);
    
              //newImage = info.;
              foreach(var i in info)
              {
                  var k = i.ToString();
              }
    
          }
    
          public override void Canceled(UIImagePickerController picker)
          {
              var cb = _callback;
              _callback = null;
    
              picker.DismissViewController(true, (Action)null);
              cb(null);
          }
      }
    
      public static void TakePicture(UIViewController parent, Action<NSDictionary> callback)
      {
          Init();
          picker.SourceType = UIImagePickerControllerSourceType.Camera;
          _callback = callback;
          parent.PresentViewController((UIViewController)picker, true, (Action)null);
      }
    
      public static void SelectPicture(UIViewController parent, Action<NSDictionary> callback)
      {
          Init();
          picker.SourceType = UIImagePickerControllerSourceType.PhotoLibrary;
          _callback = callback;
          parent.PresentViewController((UIViewController)picker, true, (Action)null);
      }
    

    }
    `

    It all works fine, the problem is that I do not know how to save the picture to the given folder and rename the file to the name I want.

    The screen shows what I have in the NSDictionary info:

    Please, let me know you managed to access the camera and take a pic ture ?
    I am looking for a way to do the same iOS App and let me know using what have you managed to debug the App

Sign In or Register to comment.