如何使用UIImage.FromImage(context.ToImage())防止内存泄漏? [英] How do I prevent a memory leak with UIImage.FromImage(context.ToImage())?

查看:149
本文介绍了如何使用UIImage.FromImage(context.ToImage())防止内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下功能,可以将像素的字节数组转换为图像.但是,我在该行中有内存泄漏:

I have the following function to convert a byte array of pixels into an image. However, I have a memory leak in the line:

unpackedImage = UIImage.FromImage(context.ToImage());

unpackedImage = UIImage.FromImage(context.ToImage());

当我注释掉上面的行时,泄漏消失了.泄漏非常严重,iOS在启动后约30秒内杀死了我的应用程序.因为这行代码.

When I comment out the line above, the leak goes away. The leak is so bad that iOS is killing my app within about 30 seconds of startup. It's because of this line of code.

如何防止此内存泄漏?有没有更好的方法来做我想做的事?

How do I prevent this memory leak? Is there a better way to do what I am trying to do?

    public static void DrawCustomImage2(IntPtr buffer, int width, int height, int bytesPerRow, CGColorSpace colSpace, byte[] rawPixels, ref UIImage unpackedImage)
    {
        GCHandle pinnedArray = GCHandle.Alloc(rawPixels, GCHandleType.Pinned);
        IntPtr pointer = pinnedArray.AddrOfPinnedObject();

        // Set a grayscale drawing context using the image buffer
        CGBitmapContext context = new CGBitmapContext(pointer, width, height, 8, bytesPerRow, colSpace, CGImageAlphaInfo.None);

        // Turning off interpolation and Antialiasing is supposed to speed things up
        context.InterpolationQuality = CGInterpolationQuality.None;
        context.SetAllowsAntialiasing(false);

        try
        {
            unpackedImage = UIImage.FromImage(context.ToImage());   // Convert the drawing context to an image and set it as the unpacked image
        } finally
        {
            pinnedArray.Free();
            if (context != null)
                context.Dispose();
        }
    }

这里是概要分析屏幕截图(选中关键代码行后,选中的项目都会消失).您可以查看已检查项目(尤其是Malloc)随着时间的增长情况.

Here is the profiling screenshot (checked items all disappear when the critical line of code is commented out). You can see how the checked items (especially the Malloc) grow over time.

这是Malloc 1.50KB上的放大视图.您可以在右侧的扩展详细信息"窗格中看到它正在调用CGBitmapContextCreateImage和CGDataProviderCreateWithCopyOfData,然后是malloc.

Here is the zoom-in view on the Malloc 1.50KB. You can see in the Extended Detail pane on the right that it is calling CGBitmapContextCreateImage and CGDataProviderCreateWithCopyOfData and then a malloc.

这是Rolf建议的性能分析屏幕截图.我运行了两次图像循环.您可以看到它在第一个循环结束时清理了多余的内存,但是第二次系统清理得不够快,iOS杀死了我的应用程序(您可以在右上角看到内存不足警告标志角落).

Here is the profiling screenshot with Rolf's suggestion. I ran the image loop twice. You can see that it cleans up the extra memory at the end of the first loop, but the system didn't clean it up fast enough the second time and iOS killed my app (you can see the low memory warning flags in the top right corner).

推荐答案

这样做:

using (var pool = new NSAutoreleasePool ()) {
    using (var img = context.ToImage ()) {
        unpackedImage = UIImage.FromImage (img); 
    }
}

这篇关于如何使用UIImage.FromImage(context.ToImage())防止内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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