Xamarin Forms iOS分享Instagram照片不起作用

时间:2019-08-07 14:20:12

标签: image xamarin.forms xamarin.ios share

我无法将图片分享到Instagram,不知道为什么。

我正在使用Xamarin Forms,并且我正在尝试使用DependencyService共享图像到Instagram,以允许在Project.iOS中调用函数

我正在ios模拟器上测试该应用程序

首先,我尝试从Uri获取UIImage:

uri = https://oc.ocstatic.com/images/favicon/android-chrome-512x512.png

public static UIImage FromUrl(string uri)
                    {
                        var imageName = uri.Substring(uri.LastIndexOf('/') + 1);

                        if (File.Exists(Path.Combine(NativeImagesPath, imageName)))
                            return UIImage.FromFile(Path.Combine(NativeImagesPath, imageName));

                        if (File.Exists(Path.Combine(ImagesCachePath, imageName)))
                            return UIImage.FromFile(Path.Combine(ImagesCachePath, imageName));

                        if (Items.ContainsKey(uri))
                            return Items[uri];

                        using (var url = new NSUrl(uri))
                        using (var data = NSData.FromUrl(url))
                        {
                            var image = UIImage.LoadFromData(data);

                            if (!NSFileManager.DefaultManager.FileExists(ImagesCachePath))
                                NSFileManager.DefaultManager.CreateDirectory(ImagesCachePath, false, null);

                            var result = NSFileManager.DefaultManager.CreateFile(Path.Combine(ImagesCachePath, imageName), data, new NSFileAttributes());
                            if (!result)
                                Items[uri] = image;

                            return image;
                        }
                    }

然后,我想使用以下代码将此UIImage共享给Instagram:


var imageURL = FromUrl(path);
NSString documentDirectory = (new NSString(Environment.GetFolderPath(Environment.SpecialFolder.Personal)));
                string saveImagePath = documentDirectory.AppendPathComponent(new NSString(@"Image.ig"));
NSData imageData = imageURL.AsPNG();
imageData.Save(saveImagePath, true);
NSUrl imageURLfinal = NSUrl.FromFilename(saveImagePath);

if (UIApplication.SharedApplication.OpenUrl(instagramURL)) {

UIDocumentInteractionController DocumentController = new UIDocumentInteractionController();
DocumentController.Delegate = new UIDocumentInteractionControllerDelegateClass(this);
DocumentController.Url = imageURLfinal;
DocumentController.Uti = "com.instagram.photo";

DocumentController.PresentOpenInMenu(new RectangleF(1, 1, 1, 1), this.View, true);

  returnMessage = "NSURL: " + path;
}
else
{
  returnMessage = "Instagram Not Found";
}
````

I expect the Instagram screen.

1 个答案:

答案 0 :(得分:0)

我也在Xamarin论坛上提出了问题,我刚刚在这里为我发布了解决问题的方法。

https://forums.xamarin.com/discussion/comment/386163#Comment_386163