Sharing image on Instagram iOS, sometimes get the last image of gallery and not the downloaded image

Daniel 196 Reputation points
2021-10-05T11:12:35.273+00:00

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) => {
                }
            );
        }
Developer technologies | .NET | Xamarin
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 36,436 Reputation points Microsoft External Staff
    2021-10-08T08:51:29.237+00:00

    Hello,
    Welcome to our Microsoft Q&A platform!
    According to your code , you save the image into the sandbox folder. However, you share the image from user’s photo library, you could try to save the image to photos album, then you could use PHPhotoLibrary.SharedPhotoLibrary.PerformChanges

    image.SaveToPhotosAlbum((img, error) =>  
                {  
      
                });  
    

    In addition, you need to add photo library usage in the info.plist:

    <key>NSPhotoLibraryUsageDescription</key>  
    <string>The app will use your photo library</string>  
    

    Best Regards,
    Wenyan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Daniel 196 Reputation points
    2021-10-08T09:49:04.577+00:00

    @Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) if I want to do the same with a video ?

    I found UIVideo.SaveToPhotosAlbum and it works like the image.SaveToPhotosAlbum but, with the url of the downloaded file ?

    I'm trying to do that, but it seems that doesn't work, but I don't know why, and I don't found examples...

    var videoName = url.Substring(url.LastIndexOf('/') + 1);  
    string returnPathStr = Path.Combine(ImagesCachePath, videoName);  
      
    UIVideo.SaveToPhotosAlbum(returnPathStr, ((returnPathStr, error) => {  
    }));  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.