我需要在xamarin.ios中的“按钮单击”上发送带有附件的电子邮件。但是UIImage.FromFile(filename)返回null。图像是从api加载并内部存储的。
以下是代码:
partial void SendMailBtn_TouchUpInside(UIButton sender)
{
var img = SelectedBook.image;
if (MFMailComposeViewController.CanSendMail)
{
if (MFMailComposeViewController.CanSendMail)
{
mailController = new MFMailComposeViewController();
mailController.SetToRecipients(to);
mailController.SetSubject(subject);
mailController.SetMessageBody(body, false);
var documentsDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
if (!string.IsNullOrEmpty(img))
{
string jpg = img + ".jpg";
string filename = System.IO.Path.Combine(documentsDirectory, img);
UIImage images = UIImage.FromFile(filename);
NSData imagedata = images.AsJPEG();
mailController.AddAttachmentData(imagedata, "image/jpeg", jpg);
}
mailController.Finished += (object s, MFComposeResultEventArgs args) => {
Console.WriteLine("result: " + args.Result.ToString()); // sent or cancelled
BeginInvokeOnMainThread(() => {
args.Controller.DismissViewController(true, null);
});
};
}
this.PresentViewController(mailController, true, null);
}
else
{
new UIAlertView("Mail not supported", "Can't send mail from this device", null, "OK");
}
}