How do I get the path to a Photo in ios?

Budaev Batozhab 1 Reputation point
2021-04-26T03:46:01.277+00:00

To get the path to the gallery in android, I use the following code:

Android. OS.Environment.ExternalStorageDirectory.AbsolutePath. 

I use dependency injection. After getting the path, then I get all the files (their paths) from the gallery and show them on the screen.

string externalDirectory = dependencyInjectionService.GetPublicExternalFolder();
string publicPicturesDirectory = Path.Combine(externalDirectory, "Pictures");
var paths = Directory.GetFiles(publicPicturesDirectory, "*.*", SearchOption.AllDirectories)

I have this logic in the beginning. if the file exists in the gallery, then we take its path and show it on the screen. If not, we save it in private external storage.

Is it possible to do this in ios? I need to get the path to the Photo and take the path of a specific file from there and show it on the screen..

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Cole Xia (Shanghai Wicresoft Co,.Ltd.) 6,756 Reputation points
    2021-04-26T08:34:36.83+00:00

    Hello,

    Welcome to Microsoft Q&A!

    Try to use PHImageManager.RequestImageForAsset to get image data programmatically on iOS.

       PHImageRequestOptions options = new PHImageRequestOptions();  
                   options.ResizeMode = PHImageRequestOptionsResizeMode.None;  
                   options.NetworkAccessAllowed = true;  
                   options.DeliveryMode = PHImageRequestOptionsDeliveryMode.HighQualityFormat;  
                   options.Synchronous = true;  
                   PHFetchResult result = PHAsset.FetchAssets(PHAssetMediaType.Image, null);  
         
         
                   PHImageManager manager = PHImageManager.DefaultManager;  
         
         
                   NSMutableArray images = new NSMutableArray();  
         
                   foreach(PHAsset asset in result) {  
         
                       manager.RequestImageForAsset(  
                           asset,  
                           PHImageManager.MaximumSize,  
                           PHImageContentMode.Default,  
                           options,  
                           (image,info) =>  
                           {  
                               images.Add(image);  
                           });  
                   }  
    

    Note : the method is fetching the image data from iCloud, so test on a real device .

    Refer to

    https://stackoverflow.com/questions/12633843/get-all-of-the-pictures-from-an-iphone-photolibrary-in-an-array-using-assetslibr
    https://stackoverflow.com/questions/31037859/phimagemanager-requestimageforasset-returns-nil-sometimes-for-icloud-photos

    Best Regards,
    Cole Xia


    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.

    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.