【发布时间】:2021-01-06 18:21:51
【问题描述】:
我有一种方法可以获取之前下载到手机图库中的图像并将其附加到电子邮件中。这在 Android 中完美运行。但在 iOS 中,我无法弄清楚如何获取我刚刚保存到图库中的图像的文件路径。
这是目前的 Xamarin.IOS 代码:
public string SaveToGallery(byte[] imgBytes, string fileName)
{
var imageData = new UIImage(NSData.FromArray(imgBytes));
string filePath = string.Empty;
NSError errorMsg = null;
imageData.SaveToPhotosAlbum((image, error) =>
{
if (error != null)
errorMsg = error;
});
if (errorMsg is null)
{
return string.Empty;
}
return filePath;
}
我使用 Xamarin.Essentials.Email 创建电子邮件,EmailAttachment 类将字符串 fullPath 作为参数。 SaveImg() 方法使用 DependencyService 调用上面保存图像的 Xamarin.iOS 代码,并应返回保存图像的文件路径:
private static async Task EmailImg(List<string> recipients, string subject, string body, byte[] imgBytes)
{
try
{
var message = new EmailMessage
{
To = recipients,
Subject = subject,
Body = body,
//Cc = ccRecipients,
//Bcc = bccRecipients
};
string filePath = string.Empty;
if (imgBytes != null)
{
filePath = await SaveImg(imgBytes);
if (!string.IsNullOrEmpty(filePath))
message.Attachments.Add(new EmailAttachment(filePath));
}
await Email.ComposeAsync(message);
}
catch (FeatureNotSupportedException fbsEx)
{
await Application.Current.MainPage.DisplayAlert("Oops", "Email function not supported on this device", "Okay");
}
catch (Exception ex)
{
await Application.Current.MainPage.DisplayAlert("Oops", "Failed to pass details to email client", "Okay");
}
}
【问题讨论】:
标签: c# xamarin xamarin.ios