xamarin-macios icon indicating copy to clipboard operation
xamarin-macios copied to clipboard

UIImage.FromFile returns null even the image exists in the resource folder

Open last-Programmer opened this issue 1 year ago • 11 comments

Steps to Reproduce

  1. Create a .net 8 ios project
  2. Add .net 8 ios class library project and add a image to the resources folder
  3. Add reference of this library project to the main ios project
  4. add <UseMaui>true</UseMaui> in both csporj file
  5. In the class library project try to use UiImage.FromFile for the image you have added

Expected Behavior

UIImage.FromFile should return the image

Actual Behavior

UIImage.FromFile return null

Environment

Version information
Visual studio 2022 in Windows Paired to sonma mac with xcode 15 installed
This does not happen in  Visual Studio for Mac.

Example Project (If Possible)

iOSAppImageTest.zip

Workaround Installing Maui nuget pacakges and removing the UseMaui tag in the library project fixed the problem. this way we can use the native embedding.

last-Programmer avatar Nov 24 '23 13:11 last-Programmer

Your project works fine for me, both in the simulator and on device.

Can you check if the image is in the app bundle (the bin/Debug/net8.0-ios/ios-*/iOSAppImageTest.app/Backgrounds directory) after the build?

Also, could you get a binlog from the build and attach it here?

rolfbjarne avatar Nov 27 '23 13:11 rolfbjarne

@rolfbjarne Thank you for the reply.

Can you check if the image is in the app bundle (the bin/Debug/net8.0-ios/ios-*/iOSAppImageTest.app/Backgrounds directory) after the build?

It did not end up there. It is missing. This is happening only in Visual studio 202 for windows.

I will post the binlogs soon.

last-Programmer avatar Nov 27 '23 14:11 last-Programmer

I tried fresh install Visual Studio 2022 in WIndows and i am still facing the issues

Here are hte binlogs iOSAppImageTest_Debug_AnyCPU_Build_2023-11-27T20_11_27.5039944+00_00.zip

last-Programmer avatar Nov 27 '23 20:11 last-Programmer

OK, I can reproduce, I'll have a look.

rolfbjarne avatar Nov 30 '23 11:11 rolfbjarne

The reason for the failure is that the library project is built locally, even if a remote connection is established.

Binlog: CentralNode_devenv_PID=13216_x64_BuildManager_Default.binlog.zip

@emaf @mauroa is that the expected behavior (to build a referenced library project with IsMacEnabled=false, even if a remote connection is available)?

rolfbjarne avatar Dec 20 '23 16:12 rolfbjarne

The reason for the failure is that the library project is built locally, even if a remote connection is established.

Just to expand a bit: the way this is supposed to work is:

  1. The library project's PackLibraryResources target is executed, embedding the resources into the library assembly.
  2. The executable project's UnpackLibraryResources target will then detect, unpack and remove any such resources from any referenced libraries.

The problem is that 1. doesn't happen, because:

Screenshot 2023-12-20 at 17 34 35

rolfbjarne avatar Dec 20 '23 16:12 rolfbjarne

After talking with @emaf and @mauro, the current behavior is indented (referenced library projects aren't built remotely) because otherwise the build speed would suffer greatly for everybody for the relatively few projects that would not build correctly.

One option we discussed was to add support for a MSBuild property that could be added to the library project to indicate that it needs to build remotely.

Another option is to pack the library resources on the Windows machine without the need for a Mac. This would probably be the best option if we could make it work, but it's a much bigger effort (see https://github.com/xamarin/xamarin-macios/issues/19028).

rolfbjarne avatar Dec 20 '23 21:12 rolfbjarne

Same issue as OP, slightly different situation. .Net Maui app in .Net 8, Visual Studio 2022 for Windows (17.8.3). Same symptoms, my file is located in Resources/Images yet UIImage.FromFile returns null. Same call works fine on the dotnet_bot.png file, which displays in the simulator.

C:\Dev\LaundryWatcher\LaundryWatcher\bin\Debug\net8.0-ios\ iossimulator-arm64 \LaundryWatcher.app does have the file I need, but C:\Dev\LaundryWatcher\LaundryWatcher\bin\Debug\net8.0-ios\ iossimulator-x64 \LaundryWatcher.app does not have the file.

I note that the PNG files I see in these folders are all zero-length anyway, including the dotnet_bot.png which FromFile appears to find nonetheless.

This would be a good one to fix. I need it for drag images.

billn6 avatar Jan 08 '24 00:01 billn6

@billn6 does the missing image come from a referenced library project, or is it included in the executable project?

rolfbjarne avatar Jan 08 '24 07:01 rolfbjarne

Hi @rolfbjarne - It is included in the executable project itself. Pretty stock .Net Maui app project with the image in question in Resources/Images.

billn6 avatar Jan 08 '24 15:01 billn6

Hi @rolfbjarne - It is included in the executable project itself. Pretty stock .Net Maui app project with the image in question in Resources/Images.

OK, then it's a different problem than the one reported here. Could you file a new issue and attach a test project we can use to reproduce the problem?

rolfbjarne avatar Jan 08 '24 15:01 rolfbjarne