【问题标题】:Xamarin iOS post image to InstagramXamarin iOS 将图像发布到 Instagram
【发布时间】:2022-01-04 11:32:24
【问题描述】:

目标:使用 iOS 原生方法,用 C# 将用户制作的图片推送到他们的 Instagram 提要。

public bool ShareImage(byte[] imageByte)
        {
            bool result = false;

            //string fileName = "xxx.png";
            //string path = Path.Combine(FileSystem.CacheDirectory, fileName);
            //File.WriteAllBytes(path, imageByte);
            NSData data = NSData.FromArray(imageByte);
            UIImage image = UIImage.LoadFromData(data);
            
            //NSUrl url = NSUrl.FromString($"instagram://library?AssetPath={path}"); // Only opens
            NSUrl url = NSUrl.FromString($"instagram://library?LocalIdentifier={1}");
            if (UIApplication.SharedApplication.CanOpenUrl(url))
            {
                UIApplication.SharedApplication.OpenUrl(url);
            }
            return result;
        }

据我所知,我必须这样做的方法是将我的图像保存到设备并为此获取本地标识符。 我是根据我看到的 Objective-c 代码的 sn-ps 构建的。共享照片仅适用于安装了本机应用程序,正如我从我的试验中了解到的那样,可以让 facebook 模块正常工作。

编辑:使用 iOS Photos 命名空间中的 PHAssetChangeRequest 不起作用。 一位同事向我指出了保存然后使用照片选择器获取本地标识符的 PHAsset 的可能性。但这是一个额外的步骤,我不希望应用程序的用户经历。最好只删除 Instagram 支持,因为我可以通过如下所示的通用共享方法。这种方法的缺点是用户必须选择要共享的媒体。

public async void ShareImage(byte[] imageByte)
        {
            string fileName = "xxx.png";
            string path = Path.Combine(FileSystem.CacheDirectory, fileName);
            File.WriteAllBytes(path, imageByte);
            await Share.RequestAsync(
                new ShareFileRequest()
                {
                    File = new ShareFile(path),
                    Title = "xxx"
                }
                );
        }

编辑 2 使用 UIDocumentInteractionController 尝试了一种不同的方式,但它什么也没显示,也没有发布,它没有抛出任何异常来给我任何关于我做错了什么的线索。

public bool ShareImage(byte[] imageByte)
        {
            bool result = false;

            string fileName = "xxx.igo";
            string path = Path.Combine(FileSystem.CacheDirectory, fileName);
            File.WriteAllBytes(path, imageByte);
            //string caption = "xxx";
            string uti = "com.instagram.exclusivegram";
            UIImageView view = new UIImageView();
            //UIDocumentInteractionController controller = UIDocumentInteractionController.FromUrl(NSUrl.FromString(path));
            UIDocumentInteractionController controller = new UIDocumentInteractionController
            {
                //controller.Url = NSUrl.FromString(path);
                Url = NSUrl.FromFilename(path),
                Uti = uti
            };
            //CoreGraphics.CGRect viewDimensions = new CoreGraphics.CGRect(0, 0, 200, 100);
            
            _ = controller.PresentOpenInMenu(CoreGraphics.CGRect.Empty, view, true);
            //_ = controller.PresentOpenInMenu(viewDimensions, view, true);
            return result;
        }

编辑 3 使用

UIView view = new UIImageView();
            if (UIApplication.SharedApplication.KeyWindow.RootViewController is UIViewController uiController && uiController.View != null)
            {
                view = uiController.View;
            }

我能够显示可能的共享选项。我使用原生 App 登录 Instagram,但帖子没有显示。

【问题讨论】:

    标签: c# ios xamarin uiimage uidocumentinteractioncontroller


    【解决方案1】:
    1. 改变

      Url = NSUrl.FromFilename(path)

      Url = new NSUrl(path,false)

      第二种方法是指向正确的文件路径而不是文件名。

    2. 改变

      controller.PresentOpenInMenu(CoreGraphics.CGRect.Empty, view, true);

      controller.PresentOptionsMenu(UIScreen.MainScreen.Bounds, (UIApplication.SharedApplication.Delegate as AppDelegate).Window, true);

      在当前窗口内显示UIDocumentInteractionController 并设置合适的框架。

    【讨论】:

    • 我更改了 url 初始化。但是在呈现时,问题在于该类本身是从 Xamarin 调用的,并且不了解 UIImageView 并且没有使用参考。除非还有办法以某种方式获得它。
    • 您是在开发 Xamarin.Forms 还是 Xamarin.iOS?
    • Xamarin.Forms。这是从 Xamarin 页面调用的本机 iOS 类(公共类 ShareServiceInstagramiOS:IShareServiceInstagram)。我必须同时实现 iOS 和 Android 版本,这就是我只发送图像的原因。图片本身是用 Skia 修改的,byte[] 就是从这里来的。
    • 检查我的更新代码,将View更改为当前窗口。
    • 还是什么都没有。我一定在更深层次上做错了什么。窗口在那里,边界与窗口具有相同的边界值。 Xamarin 覆盖本机 iOS 不太可能出现问题。也运行本机和全屏的相机预览的尺寸具有相同的绑定尺寸。 Android 版本使用 Intent 并且布局有很大不同,因此在这里也没有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-25
    相关资源
    最近更新 更多