xamarin ios screenshot and email

2019-04-17 12:38发布

问题:

How to take screenshot and send to a email with a button ? I have did the coding for taking screenshot, but it will saved to photo album.

    partial void btnScreenshot (NSObject sender)
    {
        UIImage image;
        UIGraphics.BeginImageContext (View.Frame.Size);
        //new iOS 7 method to snapshot
        View.DrawViewHierarchy (View.Frame, true);
        image = UIGraphics.GetImageFromCurrentImageContext ();
        UIGraphics.EndImageContext ();
        //...code to save to photo album omitted for brevity
        image.SaveToPhotosAlbum((img, err) => {
            if(err != null)
                Console.WriteLine("error saving image: {0}", err);
            else
                Console.WriteLine ("image saved to photo album");
            UIAlertView alert = new UIAlertView("Screenshot", "Image saved successfully", alertDelegate, "OK", null);
            alert.Show();
                alert.Show();
        });
    }

回答1:

You can use this Xamarin tutorial for instructions on composing the email.

Then to add the screenshot, you need to use the AddAttachmentData method.

// Take the screenshot
UIGraphics.BeginImageContext(View.Frame.Size);
View.DrawViewHierarchy(View.Frame, true);
UIImage image = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();

// Don't save it to the album (unless you need to)

// Create an email
var _mailController = new MFMailComposeViewController();
_mailController.SetToRecipients(new []{"john@doe.com"});
_mailController.SetSubject("Send Screenshot");
_mailController.SetMessageBody("This is a screenshot of the app!", false);

// Add the screenshot as an attachment
_mailController.AddAttachmentData(image.AsPNG(),"image/png","Screenshot.png");

// Handle the action to take when the user completes sending the email
_mailController.Finished += ( object s, MFComposeResultEventArgs args) => {
    System.Console.WriteLine (args.Result.ToString ());
    args.Controller.DismissViewController (true, null);
};

// Show the email view
PresentViewController (_mailController, true, null);