question

BudaevBatozhab-7878 avatar image
0 Votes"
BudaevBatozhab-7878 asked RobCaplan edited

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

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..

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

ColeXia-MSFT avatar image
0 Votes"
ColeXia-MSFT answered ColeXia-MSFT edited

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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.