Search code examples
c#iosxamarin.formsuiimageasset-catalog

Can't load image from other asset catalog


I have a Xamarin Forms app which has 2 Asset Catalogs. One of them is part of a library and is called SharedAssets, the other one is part of the iOS project for the app and is called Assets. The "Assets" catalog is the newest of the 2 and for some reason I can't seem to access any of them by using

UIImage.FromBundle("ic_marker");

for the ic_marker imageset while it works completely fine when I use one of the images from the SharedAssets catalog. I've looked if I could use one of the other overload for FromBundle, but I can't seem to get it to work with any bundle I use. I've also seen FromFile and FromResource methods, but I don't know if either of those would be appropriate to use and if so how I would use them.

Since I've used the same images in another app before, I doubt something is wrong with the images themselves, but I think for some reason the Assets catalog isn't loaded in correctly.

Does someone know what I'm doing wrong or maybe have an alternative solution.


Solution

  • The solution for me was:

    UIImage.FromBundle("ic_marker", NSBundle.MainBundle, UITraitCollection.CurrentTraitCollection);
    

    Apparantly I made a mistake when I tried the other overloads for FromBundle. I passed null for the traitCollection parameter, which I thought was fine since I didn't want to give up any differing traits and since the parameter was nullable I thought it would just go for some sort of default implementation, but that was not the case.