2

I know that there are serveral questions on SO covering the topic, however, so far they didn't help me to solve the problem. I want to display an image in xamarin forms. On android it works, on iOS the image wont show up. I use asset catalog to define the image in iOS. Please see below:

Xaml:

<Image Opacity="0.4" Source="happinessfactury.png" Aspect="AspectFill" />

iOS project file:

<ImageAsset Include="Assets.xcassets\happinessfactury.imageset\Contents.json" />
<ImageAsset Include="Assets.xcassets\happinessfactury.imageset\happinessfactory@1x.png" />
<ImageAsset Include="Assets.xcassets\happinessfactury.imageset\happinessfactory@2x.png" />
<ImageAsset Include="Assets.xcassets\happinessfactury.imageset\happinessfactory@3x.png" />

contents.json

{"images": [
{
  "idiom": "universal"
},
{
  "filename": "happinessfactory@1x.png",
  "scale": "1x",
  "idiom": "universal"
},
{
  "filename": "happinessfactory@2x.png",
  "scale": "2x",
  "idiom": "universal"
},
{
  "filename": "happinessfactory@3x.png",
  "scale": "3x",
  "idiom": "universal"
},
{
  "idiom": "iphone"
},
{
  "scale": "1x",
  "idiom": "iphone"
},
{
  "scale": "2x",
  "idiom": "iphone"
},
{
  "subtype": "retina4",
  "scale": "2x",
  "idiom": "iphone"
},
{
  "scale": "3x",
  "idiom": "iphone"
},
{
  "idiom": "ipad"
},
{
  "scale": "1x",
  "idiom": "ipad"
},
{
  "scale": "2x",
  "idiom": "ipad"
},
{
  "idiom": "watch"
},
{
  "scale": "2x",
  "idiom": "watch"
},
{
  "screenWidth": "{130,145}",
  "scale": "2x",
  "idiom": "watch"
},
{
  "screenWidth": "{146,165}",
  "scale": "2x",
  "idiom": "watch"
},
{
  "idiom": "mac"
},
{
  "scale": "1x",
  "idiom": "mac"
},
{
  "scale": "2x",
  "idiom": "mac"
}],"info": {
"version": 1,
"author": "xcode"},"properties": {
"on-demand-resource-tags": [
  "happinessfactury"
]}}

EDIT 1 In Xaml I mistakenly set the source to Source="happinessfactury" which was wrong. However even with Source="happinessfactury.png" it is not working on iOS. (On android though)

EDIT 2 It's a little bit embarassing, but I solved the issue. The mistake was to reference the image in xaml by happinessfactury instead of happinessfactory which is the correct name and also the name of the image files.

user2074945
  • 537
  • 2
  • 8
  • 22

1 Answers1

0

First off, I'm a little confused as to if you are using Xamarin.Forms or Xamarin.iOS. But one possible solution when using Xamarin.forms with PCL, is to simply add the images you wish to display to an Assets folder in your Portable project. And then set the image source in code behind using a ImageSource.FromResource() like so:

MyImg.Source = ImageSource.FromResource("ProjectName.Assets.happinessfactury.png");

Another source that might help is the Book Creating Mobile Apps with Xamarin.Forms

K.Warrens
  • 35
  • 8