I have a method that takes an image that was previously downloaded to the phones gallery and attaches it to an email. This works perfectly in Android. But In iOS I can't figure out how to get the file path of the image that I literally just saved to the gallery.
Here is the Xamarin.IOS code so far:
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;
}
I'm using Xamarin.Essentials.Email to create the email, the EmailAttachment class takes string fullPath as the parameter. The SaveImg() method uses DependencyService to call the Xamarin.iOS code above which saves the image and should return the file path of where the image was saved:
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");
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…