Created
February 8, 2017 00:19
-
-
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(...)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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