Forum Xamarin.Forms

Failed to allocate a XXX byte allocation with XXX free bytes and XXMB until OOM

ChongLiKenChongLiKen MYMember ✭✭
edited July 2015 in Xamarin.Forms

Dear Sir, we are trying to establish a PCL class that loading images from URL, however sometimes when the image is oversized, the system will suffer from OOM and crash. May I know how can we intercept the process, where we can validate the image sizing before we loading it into memory and hitting OOM error? We are trying to get clue from here http://developer.xamarin.com/recipes/android/resources/general/load_large_bitmaps_efficiently/ and the code, we need it workable on Android & iOS. Thanks in advanced for the help.

public class ImageCacheOptimizer
{
static Dictionary<string,ImageSource> cache = new Dictionary<string,ImageSource> ();

static public ImageSource FromFile(String filename)
{
    ImageSource retVal = null;
    bool hit = cache.TryGetValue (filename, out retVal);

    if (!hit) {
        retVal = ImageSource.FromFile (filename);
        cache [filename] = retVal;
    }

    return retVal;
}

static public ImageSource FromUri(String filename)
{
    ImageSource retVal = null;
    bool hit = cache.TryGetValue (filename, out retVal);

    if (!hit) {
        retVal = new UriImageSource { CachingEnabled = true, CacheValidity = new TimeSpan(1,0,0,0), Uri = new Uri (filename) };
        cache [filename] = retVal;
    }

    return retVal;
}

}

Sign In or Register to comment.