Xamarin.forms: [iOS] Can not load images from ios class library

Created on 21 May 2019  路  12Comments  路  Source: xamarin/Xamarin.Forms

Description

My Xamarin.Forms iOS project references an iOS lib that contains images and other stuffs, When I set Image Control's Source property to some image file from my iOS lib, the image is not displayed.

Similar question on stack overflow https://stackoverflow.com/questions/32566167/xamarin-how-to-load-image-from-ios-library-project

Steps to Reproduce

  1. Create a iOS lib and reference to startup iOS project
  2. Throw some images in to iOS lib
  3. Create an image control and set the source property to the image from iOS library.

Expected Behavior

Image should be displayed.

Actual Behavior

Nothing displayed.

Basic Information

  • Version with issue: 3.6.0.344457
  • Last known good version: 3.6.0.293080
  • IDE: Visual Studio 2019
  • Platform Target Frameworks:

    • iOS: 11.2

iOS 馃崕 question

Most helpful comment

Very strange. I might need to check that out then, I鈥檓 sure I鈥檝e seen it work.

All 12 comments

@masonyc Can you please attach a small project that demonstrates this issue? Thanks!

@masonyc Can you please attach a small project that demonstrates this issue? Thanks!

App1.zip
Hi @samhouts , I have attached the sample project above. It should display two images on the screen, one from iOS library and one from iOS project. But only the one from iOS project is shown.

OK. So, this is possible, but I think it works a little different than you expect. Find a working sample attached here based off your reproduction.

Basically what you do is described in the Docs here: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/images?tabs=macos#embedded-images

In case of your sample I had to create a new library because the one you included was an iOS specific one? So, I created a .NET Standard library for you. The principe is all the same though. Then, in that new library I added the image and set the build action to EmbeddedResource. This will give you a ID you can get your file by which is basically your library name, any folder if you have them and your filename concatenated with dots. So in this case: NewImageLib.Images.test.png.

Now, we make a reference between our shared code and this new library and we can start using the image.

Since we are loading this in a somewhat special way, this is not built into XAML (should it be @samhouts?) and we need to load the image through some code: ImageSource.FromResource("NewImageLib.Image.test.png", typeof(Class1).GetTypeInfo().Assembly). Note that I have added a dummy class Class1 to be able to resolve the right assembly so our app knows in which assembly to look for this particular image.

You can still do this with XAML as well, please refer to the earlier Docs link.

For some logistics; I don't think it's a bug but more of a question and I hope it's clear for you how to use this now. Because of this I will close the issue for now. If you feel this is not what you meant, feel free to comment of open it again.

Hope this helps!

@jfversluis What if I want set the Image in iOS lib, but allow iOS project override it if same name image exist? This is how Android works.

As far as I know, Android uses this very same mechanism in conjunction with Forms as also stated on the Docs page. But maybe I'm not understanding you correctly. Could you maybe elaborate a bit more on "how Android works" according to you?

Hi @jfversluis

image

if you have the above setup. you will see the logo.png from ClassLibrary1.

if you rename logo1.png --> logo.png in App4.Android, you will the logo.png from App4.Android

| ClassLibrary1 | App4.Android |
| -------------- | --------------- |
| Screenshot_20190614-083837 | Screenshot_20190614-084102 |

Sample Project

App4.zip

I must say, I didn't know that worked :)

I created an iOS counterpart for your sample app and it seems to work just like on Android?

App4iOS.zip

Hi @jfversluis , in your sample project, if I rename logo1.png under App4iOS.iOS to logo.png. The application still shows YouTube Image in the Library project which is not behaving the same as Android. It should show the image of Intel if I renamed the png file under my startup iOS project.

Also please note, it works before version 3.6.0.344457. Starting at 3.6.0.344457, it does not behave like that anymore.

Make sure to do a rebuild. I鈥檓 a 100% sure it worked on my end with the sample I鈥檝e attached

I am sure its a clean build. BTW your sample does not even have two logo.png file. What you had is logo1.png in iOS startup project and logo.png in Library. If you rename the logo1.png file, it does not work.

Very strange. I might need to check that out then, I鈥檓 sure I鈥檝e seen it work.

Hi @jfversluis , sorry for the misleading comment on last week. I found out that you are actually right. The problem is when we use as the build SDK on the ios Library project, the images are not carry over.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Papirosnik picture Papirosnik  路  3Comments

mfeingol picture mfeingol  路  3Comments

MartinWegner picture MartinWegner  路  3Comments

deakjahn picture deakjahn  路  3Comments

EmilAlipiev picture EmilAlipiev  路  3Comments