iOS Xamarin:将照片附加到电子邮件正文

时间:2018-03-19 17:20:49

标签: ios xamarin photo email-attachments

我正在构建一个应用程序,允许用户拍照,提取元数据并将照片附加到电子邮件+元数据信息。

然而问题是,如果您从相机捕获照片,该事件不会返回ReferenceUrl,至少我没有看到。我确实得到了某种形式的URL,但这不符合预期。这是我的代码: -

 // Take a photo with the Camera //
 partial void BtnCamera_TouchUpInside(UIButton sender)
        {
            UIImagePickerController imagePicker = new UIImagePickerController();
            imagePicker.PrefersStatusBarHidden();
            imagePicker.SourceType = UIImagePickerControllerSourceType.Camera;

            // handle saving picture and extracting meta-data from picture //
            imagePicker.FinishedPickingMedia += Handle_FinishedPickingMedia;

            // present //
            PresentViewController(imagePicker, true, () => { });

        }

要保存图像,我从FinishedPickingMedia处理程序调用SaveImageToPhone()。它看起来像这样: -

private static void SaveImagetoPhone(UIImagePickerMediaPickedEventArgs e)
{
    NSUrl url = null;
    void ImageData(PHAsset asset)
    {
        if (asset == null)
            throw new Exception("PHAsset is null");
        //PHImageManager.DefaultManager.RequestImageData(asset, null, (data, dataUti, orientation, info) =>
        //    {

        //        var urlX = info.ValueForKey(new NSString("PHImageFileURLKey"));
        //        url = info.ValueForKey(new NSString("PHImageFileURLKey")) as NSUrl;
        //        DataClass._fileURL = urlX.ToString();
        //    });
    }
    PHAsset phAsset;
    if (e.ReferenceUrl == null)
    {
        e.OriginalImage?.SaveToPhotosAlbum((image, error) =>
        {
            if (error == null)
            {
                var options = new PHFetchOptions
                {
                    FetchLimit = 1,
                    SortDescriptors = new[] { new NSSortDescriptor("creationDate", true) }
                };
                phAsset = PHAsset.FetchAssets(options).LastOrDefault() as PHAsset;
                ImageData(phAsset);
            }
        });
    }
    else
    {
        phAsset = PHAsset.FetchAssets(new[] { e.ReferenceUrl }, null).FirstOrDefault() as PHAsset;
        ImageData(phAsset);
    }
}

在这个函数中,我有几行被注释掉,在这里我试图获取Photo的参考URL(失败)。

最后我的电子邮件()看起来像这样: -

  partial void BtnMessageDone_TouchUpInside(UIButton sender)
        {
            MFMailComposeViewController mailController;
            if (MFMailComposeViewController.CanSendMail)
            {
                StringBuilder htmlBodyMail = FormatEmailBody();
                mailController = new MFMailComposeViewController();
                // do mail operations here
                mailController.SetToRecipients(new string[] { "xxyyzz@...com" });
                mailController.SetSubject("mail test");
                mailController.SetMessageBody(htmlBodyMail.ToString(), false);


                UIImage img = UIImage.FromFile(DataClass._fileURL);
                mailController.AddAttachmentData(img.AsJPEG(), "image/JPG", "Image.JPG");
                mailController.Finished += (object s, MFComposeResultEventArgs args) =>
                {
                    Console.WriteLine(args.Result.ToString());
                    args.Controller.DismissViewController(true, null);
                };
                this.PresentViewController(mailController, true, null);
            }

有没有人知道如何获取应用程序相机拍摄的照片的绝对路径/参考URL并将其附加到电子邮件正文?

1 个答案:

答案 0 :(得分:0)

使用NSUrl类上的Path属性:

NSString urlString = new NSString("file:///stack/over%20flow/foobar.txt");
NSUrl myFileUrl = new NSUrl (urlString);
Console.WriteLine (myFileUrl.AbsoluteString);
string absPath = myFileUrl.Path;
Console.WriteLine (absPath);