ios - 内存压力在 MonoTouch 中加载一堆 PNG 图像

标签 ios memory-management xamarin.ios uiimage profiling

我有一个 MonoTouch 应用程序,每 1/12 秒加载一个帧。我使用的是 UIkit,而不是 opengl。我在 View 中有一个 UIImage,并且我正在加载图像的背景任务。

一切正常,但是,一分钟后(或多或少),应用程序停止,跟踪器给我一个“低内存压力”

它在模拟器中运行良好,没有问题。我正在查看分析器,似乎内存已被处理,但是当我在 iPad 上尝试时......:(

我使用 image.Dispose() 释放内存。我在内存中有 2 张图像,显示其中一张,然后释放旧的。这种行为是可以的,因为我在 Windows Phone 上有相同的逻辑并且它工作正常。

我试过不使用 backgroundTask,而是直接从主线程加载图像。它给了我更多的时间!!!如果我使用 backgroundTask,应用程序运行 30 秒然后退出。如果我不使用 backgroundTask,它将持续 1 分钟。

我不知道该怎么办!!!!有谁能帮帮我吗?

谢谢!!!

Texture2D.FromStream 它只是 UIImage.FromFile 的包装器

这是后台 worker :

  void LoadTextureInBackground()
    {
        if (currentMovie == null) return;

        DateTime timeOnstart = DateTime.Now;

        // Mantenemos 2 texturas en memoria para no cargar sobre el mismo objeto texture.
        string fileName = currentMovie.GetFileName();

        if (lastFileName == fileName) return;

        textureLoaded[currentTexture] = Texture2D.FromStream(Device.GraphicsDevice, fileName);

        texture = textureLoaded[currentTexture];

        currentTexture = 1 - currentTexture;

        lastFileName = fileName;

        GC.Collect();
        System.Threading.Thread.Sleep(50);
    }

这是绘制方法:

    /// <summary>
    /// This is called when the game should draw itself.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    public void Draw(TimeSpan totalTime,TimeSpan elapsedTime)
    {
        if(currentMovie==null) return;

       // Mantenemos 2 texturas en memoria para no cargar sobre el mismo objeto texture.

        if (texture == null) return;

        int newWidth = (int)(texture.Width*RenderSize);
        int newHeight = (int)(texture.Height*RenderSize);

        Texture2D drawTexture = texture;

        // Al cargar usando Texture2D.FromStream, la textura NO lleva elAlpha premultiplicado

        //Device.SpriteBatch.Draw(drawTexture, destination, Color.White);
        if(CultTravel.AppDelegate.ImagePng.Image!=drawTexture.Texture)
        {
            AppDelegate.ImagePng.Image=drawTexture.Texture;
            AppDelegate.ImagePng.Frame=new System.Drawing.RectangleF(0,480-newHeight,ImagePng.Image.CGImage.Width,newHeight);

        // Si la textura que tengo cargada y lista para mostrar es diferente a la que mostraba, libero la antigua
        if (lastTextureDraw!=null && lastTextureDraw != texture)
        {
            lastTextureDraw.Dispose();
        }

        lastTextureDraw = texture;

    }

注意: 我刚刚解决了这个问题,但是我必须禁用后台工作程序,在 Draw 方法中添加加载代码并在主线程中添加 GC.Collect:

       if (lastTextureDraw!=null && lastTextureDraw != texture)
        {
            lastTextureDraw.Dispose();
            // I MUST ADD THIS TO WORK AND DISABLE THE BACKGROUND TASK!!
            GC.Collect();
        }

最佳答案

我刚刚解决了这个问题,但是我必须禁用后台工作程序,在 Draw 方法中添加加载代码并在主线程中添加 GC.Collect:

   if (lastTextureDraw!=null && lastTextureDraw != texture)
    {
        lastTextureDraw.Dispose();
        // I MUST ADD THIS TO WORK AND DISABLE THE BACKGROUND TASK!!
        GC.Collect();
    }

所以,在使用线程时出了点问题....

关于ios - 内存压力在 MonoTouch 中加载一堆 PNG 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10276361/

相关文章:

ios - 将 UIView 边缘固定到 UITableViewCell contentView 时出现布局错误

ios - UIWebView 用户代理未正确设置自定义值

c - 从内核空间访问数据段

ipad - MonoTouch 和 UIPopoverController

cocoa-touch - iPhone/iPad UIButton TitleLabel 文本未出现

iphone - ios 推特 OATH 集成不起作用

ios - 防止在 UITextField 中编辑文本并在使用 inputView 时隐藏光标/插入符号/放大镜

android - 内存不断上升而没有收到来自leakcanary的内存泄漏通知

c++ - 如何换出内存块?

ios - UINavigationController 在单击后退按钮时向下滚动