PictureMarkerSymbol in iOS Xamarin does not load an image from the local file system or included in the MainBundle

1570
5
Jump to solution
03-23-2018 03:41 PM
OscarAzmitia
New Contributor II

We have an iOS app using the 100.2.1 sdk.

We need to load an image from the MainBundle or file system to dynamically change the image to load depending on the user.

We are attempting with this code:

var url = NSBundle.MainBundle.PathForResource("StreetsIconSmall", "png", "Images");

if (url == null || url.Length == 0)
   return null;

if (!System.IO.File.Exists(url))
   return null;

return new Esri.ArcGISRuntime.Symbology.PictureMarkerSymbol(new Uri(url));

The file exists and it was compiled with buildaction "BundleResource"

This same code works just fine, if we put the same image on our server and use a web url such as http://domain/images/StreetsIconSmall.png.

Also, the image works when loaded to a UIImageView.

Any ideas?ArcGIS Runtime SDK for .NET@

0 Kudos
1 Solution

Accepted Solutions
OscarAzmitia
New Contributor II

That simply throws an invalid url exception.

Currently we found a work-around but it seems really odd that local files are not supported. Our goal is to be able to have the images in the local file system and dynamically load them.

This code below works:

UIImage img = UIImage.FromFile(url);

// url is a local file or bundle file path.

byte[] myByteArray = null;

using (NSData imageData = img.AsPNG())
{
       myByteArray = new byte[imageData.Length];
       Marshal.Copy(imageData.Bytes, myByteArray, 0, Convert.ToInt32(imageData.Length));
}

var pictureMarker = new PictureMarkerSymbol(new RuntimeImage(myByteArray));

View solution in original post

0 Kudos
5 Replies
dotMorten_esri
Esri Notable Contributor

Does a simple relative path work for you?

new Esri.ArcGISRuntime.Symbology.PictureMarkerSymbol(new Uri(("Images/StreetsIconSmall.png"));

0 Kudos
OscarAzmitia
New Contributor II

That simply throws an invalid url exception.

Currently we found a work-around but it seems really odd that local files are not supported. Our goal is to be able to have the images in the local file system and dynamically load them.

This code below works:

UIImage img = UIImage.FromFile(url);

// url is a local file or bundle file path.

byte[] myByteArray = null;

using (NSData imageData = img.AsPNG())
{
       myByteArray = new byte[imageData.Length];
       Marshal.Copy(imageData.Bytes, myByteArray, 0, Convert.ToInt32(imageData.Length));
}

var pictureMarker = new PictureMarkerSymbol(new RuntimeImage(myByteArray));

0 Kudos
dotMorten_esri
Esri Notable Contributor

Oops I forgot the relative parameter:

new Uri("Images/StreetsIconSmall.png", UriKind.Relative);

0 Kudos
OscarAzmitia
New Contributor II

We did try that and although that doesn't throw the exception. It just doesn't show up on the map.

0 Kudos
OscarAzmitia
New Contributor II

Were you able to see that there is no way to reference a local image on the device to show on a graphic? The only way we found was with the code above, but it is not as elegant as providing a local path url.

Would be nice to have that in an update.

0 Kudos