xamarin ios截图和电子邮件

时间:2014-04-29 07:32:38

标签: c# xamarin.ios xamarin

如何截取屏幕截图并发送到带有按钮的电子邮件? 我已经完成了截屏的编码,但它会保存到相册中。

    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 个答案:

答案 0 :(得分:4)

您可以使用此Xamarin tutorial获取有关撰写电子邮件的说明。

然后要添加屏幕截图,您需要使用AddAttachmentData方法。

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