UIImagePickerController is showing last clicked image

RaghavSukhwal.4674RaghavSukhwal.4674 USMember
edited November 2013 in Xamarin.iOS

In my project, when I launch camera first time it works fine. when I launch camera second time, I see image last clicked in view finder. I am not sure what's causing this.

Can anyone please help me here?


  • here is my code to launch camera.

    UIImagePickerController imagePicker = new UIImagePickerController();
    // Handle media selected.
    var documentsDirectory = Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments);
    imagePicker.FinishedPickingMedia += (sender, e) => {
    UIImage image = (UIImage)e.Info.ObjectForKey(
    new NSString("UIImagePickerControllerOriginalImage"));

            if (image != null)
                this.InvokeOnMainThread(() => {
                    this.clickedImage.Image = image;
                    image.SaveToPhotosAlbum(delegate(UIImage img, NSError err){ 
                    string pngFilename = System.IO.Path.Combine (documentsDirectory, "Photo.png"); // hardcoded filename, overwrites each time
                    NSData imgData = image.AsPNG();
                    NSError SaveErr = null;
                    if (imgData.Save(pngFilename, false, out SaveErr))
                        Console.WriteLine("saved as " + pngFilename);
                    } else {
                        Console.WriteLine("NOT saved as" + pngFilename + " because" + SaveErr.LocalizedDescription);
        // Handle cancellation of picker.
        imagePicker.Canceled += (sender, e) => {
        btnCameraDisplay1.SetTitle("Take Picture", UIControlState.Normal);
        btnCameraDisplay1.Font = UIFont.SystemFontOfSize(19);
        btnCameraDisplay1.SetTitleColor(UIColor.Black, UIControlState.Normal);
        btnCameraDisplay1.TouchUpInside += delegate(object sender, EventArgs e)
                imagePicker.SourceType = UIImagePickerControllerSourceType.Camera;
                imagePicker.AllowsEditing = false;
                this.PresentViewController(imagePicker, true,null);
                alertView = new UIAlertView();
                alertView.Message = "No camera available in this device.";
                alertView.AlertViewStyle = UIAlertViewStyle.Default;
  • Hi there,

    Any help/hint on this?

  • JamesBryanJamesBryan USMember ✭✭
    edited February 2014


    Place this after you have have used imagePicker.DismissViewController();

    I place it in the parent UIViewController. This is after I have saved the image.

    This error gave me a lot of grief but so far so good.

    Thanks to Shaddix on stack overflow. I can't believe I didn't remember that little gem myself.

  • BrianSabinoBrianSabino USMember

    James, you're a hero. Dismiss the ImagePicker, set the variable to null then invoke the Garbage Collector. That's the only way to be sure the underlying object is released. Really Xamarin should be doing this in Dismiss(), but this seems to be a reasonable workaround.

Sign In or Register to comment.