Get NSData or byte[] from the path of cameraroll/Gallery image

gayugokhalegayugokhale INMember ✭✭

I have been working on xamarin.forms from like 10 months and on its ios project from last one month.

I have created custom gallery for selecting multiple images from it and use them further in my app. I use PHAsset for loading images in collectionview.

I want to store its file path in a list because, as I want these images later in my app and I have implemented in android by this way(storing paths List<string> which is defined in PCL project) I want to do same thing in ios so that I won't need to implement separately for two different platforms and it will be easy to understand.

So, I get path like this file:///var/mobile/Media/DCIM/104APPLE/IMG_4585.PNG. I think this path is fine.

So, If I do File.ReadAllBytes(filepath) it gives filepath not found error. Surprising fact is it work in simulator. In simulator, its path is in Documents/Media/DCIM/.../IMG_001.PNG (Sorry ! I don't remember exact path. but images are in where they actually should be!)

NSData data = NSData.FromFile(filepath); // I tried FromUrl, FromString Also. In FromString I get NSData but its length is too small like 44 or 126. UIImage image = UIImage.LoadFromData(data); // image is null

After above line I do Marshal.Copy and convert it to byte[]

So, what is wrong with my code ?
can anybody please help ?



  • This issue arises because of iOS being sandboxed and restricting everything. As a result, You don't have access to the direct path of the image. A workaround is to take the PHAsset, convert it into a UIImage, and then save it to a different location (the documents folder or a temp folder). You can then take that new filepath and use it in your program. Hope that helps! :smile:

Sign In or Register to comment.