我目前正在使用 Monotouch。我有一个应用程序可以打开相机,然后需要处理当前正在捕获的图像。我使用的代码与 https://github.com/reinforce-lab/com.ReinforceLab.MonoTouch.Controls/tree/master/VideoCaptureSample/ 非常相似
我的代码和他们的代码之间的区别在于,在放置在我的 UIImagePickerController 上的 OverlayView.cs 中,我有以下代码:
using (var screenImage = CGImage.ScreenImage.WithImageInRect(Frame))
{
_imageView = UIImage.FromImage(screenImage);
Bitmap srcbitmap = new System.Drawing.Bitmap(_imageView);
}
但是,运行此代码将导致内存不断填满,直到应用程序关闭。似乎从未处理过 System.Drawing.Bitmap。我正在使用 ZXing.monotouch,所以我需要位图,以便我可以创建位图的 bytes[] 以传递到 RGBLuminanceSource
关于如何让它很好地发挥作用,我有什么想法吗?
最佳答案
如果您使用 [1] 中的代码,那么它看起来相当不完整。没有任何东西可以释放从 AllocHGlobal 分配的内存,这将导致内存不足的情况。
这不是 MonoTouch 错误 - zxing 代码应该在 Bitmap 类上实现 IDisposable 并确保在处置时(或当 GC 运行其终结器时)释放所有内容。
您可以完成类(class)(即实现 IDisposable)或复制/粘贴为您提供包含图像的 byte[] 缓冲区的部分(并在将其复制到托管缓冲区后释放非托管内存)。
[1] https://github.com/JohnACarruthers/zxing.MonoTouch/blob/master/Bitmap.cs
关于c# - Zxing 的 System.Drawing.Bitmap 中可能存在内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6977278/