Reputation: 214
I am using Image control in Xamarin forms android app. Used at two places one on hamburger slider and another on a content page. I am resolving image from web API and using the code below:
private void OnPresentedChanged(object sender, EventArgs e)
{
ImgProfile.Source = new UriImageSource()
{
Uri = new Uri(Constants.ProfilePicUrl),
CachingEnabled = true,
CacheValidity = new TimeSpan(5,0,0,0)
};
}
I tried the above code with both the conditions CachingEnabled = true/false
. Here is what I observed:
CachingEnabled = False
: Each time the image control flickers and reloads the image. I can see a time gap of second or two between image reload from web URL. Similarly in the slider menu.
CachingEnabled = true
: My image control keeps on displaying the cached version even if the URL has a newer/different image, as its profile page and the user can change his/her profile image N times a day.
So #1 solves my problem but the flickering part is annoying.
Please note, my Image control is native xamarin forms image control. Also, the profile image is taken and uploaded from the camera so there no way of uploading a customized image with lesser size to eliminate the delay.
I hope, my problem is clear to you guys.
Upvotes: 1
Views: 883
Reputation: 15816
//
// Summary:
// Gets or sets a System.TimeSpan structure that indicates the length of time after
// which the image cache becomes invalid.
public TimeSpan CacheValidity { get; set; }
Why are you setting the CacheValidity
to 5 days? That means your cached image is always valid until 5 days later.
I think that's why your image control keeps on displaying the cached version even if the url has newer/different image
.
Try to give a small value of CacheValidity and load again, something like:
// 10 seconds
CacheValidity = new TimeSpan(0, 0, 0, 10);
// 10 minutes
CacheValidity = new TimeSpan(0, 0, 10, 0);
// 1 hour
CacheValidity = new TimeSpan(0, 1, 0, 0);
Upvotes: 1