UIImagePickerController is showing last clicked image

RaghavSukhwal.4674RaghavSukhwal.4674 Raghav SukhwalUSMember
edited November 2013 in 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?

Answers

  • RaghavSukhwal.4674RaghavSukhwal.4674 Raghav Sukhwal USMember

    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);
                    }
                });
            }
    
            DismissViewController(true,null);
    
        };
    
        // Handle cancellation of picker.
        imagePicker.Canceled += (sender, e) => {
            DismissViewController(true,null);
    
        };
    
    
    
        btnCameraDisplay1.SetTitle("Take Picture", UIControlState.Normal);
        btnCameraDisplay1.Font = UIFont.SystemFontOfSize(19);
        btnCameraDisplay1.SetTitleColor(UIColor.Black, UIControlState.Normal);
    
        btnCameraDisplay1.TouchUpInside += delegate(object sender, EventArgs e)
        {
            if(UIImagePickerController.IsSourceTypeAvailable(UIImagePickerControllerSourceType.Camera))
            {
                imagePicker.SourceType = UIImagePickerControllerSourceType.Camera;
                imagePicker.AllowsEditing = false;
                this.PresentViewController(imagePicker, true,null);
            }
            else{
                alertView = new UIAlertView();
                alertView.AddButton("OK");
                alertView.Message = "No camera available in this device.";
                alertView.AlertViewStyle = UIAlertViewStyle.Default;
                alertView.Show();
            }
        };
    
  • RaghavSukhwal.4674RaghavSukhwal.4674 Raghav Sukhwal USMember

    Hi there,

    Any help/hint on this?

  • JamesBryanJamesBryan James Bryan USMember
    edited February 2014

    GC.Collect()

    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 Brian Sabino 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.