xamarin / Xamarin.Forms

Xamarin.Forms is no longer supported. Migrate your apps to .NET MAUI.
https://aka.ms/xamarin-upgrade
Other
5.62k stars 1.87k forks source link

[iOS] Can not load images from ios class library #6231

Closed masonyc closed 5 years ago

masonyc commented 5 years ago

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

samhouts commented 5 years ago

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

masonyc commented 5 years ago

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

jfversluis commented 5 years ago

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!

thudugala commented 5 years ago

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

jfversluis commented 5 years ago

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?

thudugala commented 5 years ago

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

jfversluis commented 5 years ago

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

masonyc commented 5 years ago

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.

jfversluis commented 5 years ago

Make sure to do a rebuild. I’m a 100% sure it worked on my end with the sample I’ve attached

masonyc commented 5 years ago

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.

jfversluis commented 5 years ago

Very strange. I might need to check that out then, I’m sure I’ve seen it work.

masonyc commented 5 years ago

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.