Xamarin.Android 中使用位图的 OutOfMemoryException

OutOfMemoryException using Bitmaps in Xamarin.Android

我在尝试下载图像(可以是大图像)时遇到 OutOfMemoryException。我正在使用 Xamarin.Android 和 PCL 进行跨平台操作。

我想制作图片幻灯片。我的布局上叠加了有限数量的图像视图。当我刷出所有图像后,我会在这些图像视图中重新加载新图像。我做了一个简单的项目,只是机制的刷新部分。

请不要客气,我是 Android 和 Xamarin 的初学者,这是代码:

MainActivity.cs:

[Activity(Label = "App1", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        private string[] urls = {
                                    "http://momotaros.fr/wp-content/uploads/2014/01/One-Piece-5.png",
                                    "http://www.infinite-rpg.com/wp-content/uploads/2014/09/Equipage.png",
                                    "http://ekladata.com/ke33JLPQ2tTW1mTHCvPJBrKCPOE.jpg",
                                    "http://ekladata.com/P2Q1mQuYTDbfEZnwb3f3IBsXoGM.png",
                                    "http://ekladata.com/9QUE66iKU9uGUPVUdFmPF_ZIkK8.png"
                                };
        protected override void OnCreate(Bundle bundle)
        {
            BlobCache.ApplicationName = "App1";
            BlobCache.EnsureInitialized();
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            // Get our button from the layout resource,
            // and attach an event to it
            Button button = FindViewById<Button>(Resource.Id.MyButton);
            ImageView image = FindViewById<ImageView>(Resource.Id.imageView1);
            ImageView image2 = FindViewById<ImageView>(Resource.Id.imageView2);
            ImageView image3 = FindViewById<ImageView>(Resource.Id.imageView3);
            ImageView image4 = FindViewById<ImageView>(Resource.Id.imageView4);
            ImageView image5 = FindViewById<ImageView>(Resource.Id.imageView5);
            button.Click += (sender, e) =>
            {
                image.SetImageURL(urls[0]);
                image2.SetImageURL(urls[1]);
                image3.SetImageURL(urls[2]);
                image4.SetImageURL(urls[3]);
                image5.SetImageURL(urls[4]);
            };
        }
    }
    public static class BitmapExtensions
    {
        public static async void SetImageURL(this ImageView imageView, string url)
        {
            IBitmap bmp = await App1.Shared.ImageDownloadService.DownloadImage(url, imageView.Width, imageView.Height);
            if (bmp != null)
            {
                imageView.SetImageDrawable(bmp.ToNative());
            }
        }
    }

ImageDownloadService.cs(在PCL):

public class ImageDownloadService
    {
        public static async Task<Splat.IBitmap> DownloadImage(string url, float desiredWidth, float desiredHeight)
        {
            return await BlobCache.LocalMachine.LoadImageFromUrl(url,false, desiredHeight:desiredHeight,desiredWidth: desiredWidth);
        }
    }

当第一次点击该按钮时就可以了,下载图像(即使我发现 DDMS 中的内存使用率有点高)。

但是对于下一次点击,观察内存使用情况,它会像地狱一样增加。

我在想,当我在那个 imageView 中设置一个新图像时,内存中以前的位图没有被释放,在某处有很强的参考意义,但我找不到它在哪里。

非常感谢您对这个问题的帮助或调试内存使用的任何技术,例如跟踪对象创建位置和销毁位置的方法。

感谢您花时间阅读这篇文章post,希望您能帮帮我。

在您的 BitmapExtensions 中,您可能应该在将 Bitmap 分配给 ImageView 后立即处理它,因为之后您不会将它用于任何其他用途。

public static class BitmapExtensions
{
    public static async void SetImageURL(this ImageView imageView, string url)
    {
        using (IBitmap bmp = await App1.Shared.ImageDownloadService.DownloadImage(url, imageView.Width, imageView.Height))
        {
            if (bmp != null)
            {
                using(var nativeBmp = bmp.ToNative())
                    imageView.SetImageDrawable(nativeBmp);
            }
        }
    }
}

你还应该确保 DownloadImage 方法捕获所有抛入其中的 Exceptions ,否则如果发生的话你会很糟糕。

调用 GC.Collect 应该不是必需的,因为您随后会强制 GC 在那一刻进行收集,这可能会使应用程序在执行其操作时无响应。一定要处理掉你所有的参考资料,你应该是金色的。