Home > Mobile >  Unable to add image to xcassets in Xamarin.iOS project
Unable to add image to xcassets in Xamarin.iOS project

Time:06-06

I have recently updated to Visual Studio for Mac 2022 and since then I have been having the following issue: I am unable to add new images to the Media.xcassets folder

On VS for Mac 2019 I was able to achieve this within Visual Studio, now when I double click the xcassets folder in the left panel browser, the file is opened in Xcode. I can add a new image set in Xcode but the change is not reflected when I clean, build & deploy the application from Visual Studio. Any instances of the new image I use on xib files or load programatically e.g. UIImage.FromBundle("new_image") do not display in the app. Images that were previously added to the project using VS for Mac 2019 display correctly.

This appears to be a bug with VS for Mac 2022, so really I'm asking if anyone is aware of a workaround?

CodePudding user response:

This appears to be caused by a bug in VS 2022. I worked around this problem with the following steps:

  1. Make sure your Media.xcassets folder is in the root directory of your Xamarin iOS project, the same place as your .csproj file eg. MyApp/MyApp.iOS/Media.xcassets - in my case, it was in MyApp/MyApp.iOS/Resources/Media.xcassets so I had to move it

  2. Double click the Media.xcassets file in Visual Studio to open it in Xcode

  3. Add a new image in Xcode and save

  4. When returning to visual studio you should see "Error synchronizing changes from Xcode" in the bottom left of the VS window (the VS bug)

  5. Update your iOS project file (MyApp/MyApp.iOS/MyApp.iOS.csproj) to include references to both the Contents.json and the pdf/png file for your new image. For example I had to add these two lines:

    <ImageAsset Include="Media.xcassets\new_image.imageset\Contents.json" /> 
    <ImageAsset Include="Media.xcassets\new_image.imageset\new_image.pdf" />
    
  6. When you next build and deploy, the new image should appear in the app

  • Related