I'm using this code to download the image of an uri and then pass it to the function that opens Instagram like a UImage:
DocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
NativeImagesPath = "ChannelLogos";
ImagesCachePath = Path.Combine(DocumentsPath, "ChannelLogos");
Items = new Dictionary<string, UIImage>();
public static UIImage FromUrl(string uri) {
var imageName = uri.Substring(uri.LastIndexOf('/') + 1);
if (File.Exists(Path.Combine(NativeImagesPath, imageName)))
{
returnPath = Path.Combine(NativeImagesPath, imageName);
return UIImage.FromFile(Path.Combine(NativeImagesPath, imageName));
}
if (File.Exists(Path.Combine(ImagesCachePath, imageName)))
{
returnPath = 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());
returnPath = Path.Combine(ImagesCachePath, imageName);
if (!result)
Items[uri] = image;
if (result)
image = UIImage.FromFile(returnPath);
Console.WriteLine("Image path: " + returnPath);
return image;
}
}
The problem comes when I try to share another image, the application gets me the last image that I use, and not the one that I'm downloading or selecting. After that, I use that function to share to Instagram, and I need to have the correct image, to share correctly to Instagram.
public void FinishedPickingImage(UIImage image){
PHPhotoLibrary.SharedPhotoLibrary.PerformChanges(
() => {
PHAssetChangeRequest request = PHAssetChangeRequest.FromImage(image);
string id = request.PlaceholderForCreatedAsset.LocalIdentifier;
var shareURL = "instagram://library?LocalIdentifier=" + id;
BeginInvokeOnMainThread(() =>
{
//UIApplication.SharedApplication.OpenUrl(new NSUrl(shareURL));
if (UIApplication.SharedApplication.CanOpenUrl(new NSUrl(shareURL)))
{
UIApplication.SharedApplication.OpenUrl(new NSUrl(shareURL));
Console.WriteLine("Instagram ShareURL: -> " + new NSUrl(shareURL));
}
else
{
Console.WriteLine("(Not Instagram)but ShareURL: -> " + new NSUrl(shareURL));
}
});
},
(bool success, NSError error) => {
}
);
}