加载图像时出现OutOfMemoryError [英] OutOfMemoryError when loading an image

查看:68
本文介绍了加载图像时出现OutOfMemoryError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示图像的简单页面.源是URL

I have a simple page that displays an image.The source is a URL

   var img = new Image ();
    var source = new UriImageSource {
        Uri = new Uri (string.Format ("http://xxxx.com/imagem/?{0}", url)),
            CachingEnabled = false
        };
        img .Source = source;

但是当我第四次或第五次访问此页面()时,出现此错误

but when I access this page (), the fourth time or the fifth it, I get this error

java.lang.OutOfMemoryError
    at android.graphics.Bitmap.nativeCreate(Native Method)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:928)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:901)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:868)
    at md5530bd51e982e6e7b340b73e88efe666e.ButtonDrawable.n_draw(Native Method)
    at 340b73e88efe666e.ButtonDrawable.draw(ButtonDrawable.java:49)
    at android.view.View.draw(View.java:15235)
    at android.view.View.getDisplayList(View.java:14141).....

推荐答案

我们遇到了一个非常相似的问题.看来 Xamarin已发布了有关该问题的指南,但事实并非如此.真正适合Xamarin Forms.基本上,在设置图像源之前,您需要确定是否应按比例缩小图像并手动进行.这可能需要自定义渲染器.

We ran into a very similar issue. It appears Xamarin has released some guidance on the issue, but it's not really geared towards Xamarin Forms. Basically, before setting the source of the image, you need to decide whether it should be scaled down, and do it manually. This will likely require a Custom Renderer.

似乎Xamarin Forms应该为我们做些事情,但有时您必须以我认为的好事来承担坏事.

Seems like something Xamarin Forms should do for us, but sometimes you have to take the bad with the good I suppose.

此处是一个讨论在Xamarin论坛上有关此问题的信息.

Here is a discussion on the Xamarin Forums regarding this issue.

更新: 我只是注意到您说这只会在页面加载第4或第5次之后发生.我们在页面上创建了一种解决方法,以解决该问题.基本上,您必须将图像的源"设置回null并在页面消失时强制进行垃圾回收.像这样:

Update: I just noticed you said that this happens only after the 4th or 5th time the page is loaded. We created a workaround on the page that solved the issue. Basically, you have to set the image's Source back to null and force a garbage collection when the page is disappearing. Like this:

protected override void OnDisappearing ()
{
    base.OnDisappearing ();
    img.Source = null;
    GC.Collect ();
}

这篇关于加载图像时出现OutOfMemoryError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆