目標:使用 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 代碼片段構建的。共享照片僅適用于安裝了本機應用程式,正如我從我的試驗中了解到的那樣,讓 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;
}
我能夠顯示可能的共享選項。我使用本機應用程式登錄到 Instagram,但帖子沒有顯示。
uj5u.com熱心網友回復:
改變
Url = NSUrl.FromFilename(path)到
Url = new NSUrl(path,false)第二種方法是指向正確的檔案路徑而不是檔案名。
改變
controller.PresentOpenInMenu(CoreGraphics.CGRect.Empty, view, true);到
controller.PresentOptionsMenu(UIScreen.MainScreen.Bounds, (UIApplication.SharedApplication.Delegate as AppDelegate).Window, true);UIDocumentInteractionController在當前視窗內顯示并設定正確的框架。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/409433.html
標籤:
