Skip to content

Instantly share code, notes, and snippets.

@teamtam
Created February 8, 2017 00:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save teamtam/953ab0ff17c4ec11487e759493f0bc12 to your computer and use it in GitHub Desktop.
Save teamtam/953ab0ff17c4ec11487e759493f0bc12 to your computer and use it in GitHub Desktop.
Manipulates a URI to get a resolution appropriate image by convention (for iOS) before calling Xamarin.Forms.ImageSource.FromUri(...)
public class UriImageLoaderIos : IUriImageLoader
{
public string GetUri(string baseImageUri)
{
if (string.IsNullOrEmpty(baseImageUri))
{
throw new ArgumentNullException("baseImageUri");
}
if (UIScreen.MainScreen.Scale > 2.0)
{
return InsertMultiplierBeforeSuffix(baseImageUri, "@3x");
}
else if (UIScreen.MainScreen.Scale > 1.0)
{
return InsertMultiplierBeforeSuffix(baseImageUri, "@2x");
}
else
{
return baseImageUri;
}
}
private string InsertMultiplierBeforeSuffix(string baseImageUri, string multiplier)
{
var lastDotIndex = baseImageUri.LastIndexOf(".");
if (lastDotIndex == -1)
{
throw new ArgumentException("Invalid file name: " + baseImageUri, baseImageUri);
}
return baseImageUri.Insert(lastDotIndex, multiplier);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment