gpt4 book ai didi

ios - 在使用 Mono Touch 的循环中使用 CGImage.ScreenImage 时出现内存问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:19:41 24 4
gpt4 key购买 nike

我正在尝试创建一个应用程序来使用 Zxing 的 Monotouch 和 C# 端口读取 QR 码,但我遇到了内存问题。当应用程序处理捕获的屏幕帧时,应用程序会收到内存警告,然后关闭。我已经删除了对 Zxing 的调用以追踪内存问题的根源,并且可以通过循环捕获屏幕图像来重现该问题。

代码如下:

using System;
using System.Drawing;
using System.Collections.Generic;
using System.Threading;
using MonoTouch.UIKit;
using MonoTouch.Foundation;
using MonoTouch.CoreGraphics;
using com.google.zxing;
using com.google.zxing.common;
using System.Collections;
using MonoTouch.AudioToolbox;
using iOS_Client.Utilities;

namespace iOS_Client.Controllers
{

public class CameraOverLayView : UIView
{

private Thread _thread;
private CameraViewController _parentViewController;
private Hashtable hints;
private static com.google.zxing.MultiFormatReader _multiFormatReader = null;
private static RectangleF picFrame = new RectangleF(0, 146, 320, 157);
private static UIImage _theScreenImage = null;


public CameraOverLayView(CameraViewController parentController) : base()
{
Initialize();
_parentViewController = parentController;
}

private void Initialize()
{

}

private bool Worker()
{


Result resultb = null;

if(DeviceHardware.Version == DeviceHardware.HardwareVersion.iPhone4
|| DeviceHardware.Version == DeviceHardware.HardwareVersion.iPhone4S)
{
picFrame = new RectangleF(0, 146*2, 320*2, 157*2);

}

if(hints==null)
{
var list = new ArrayList();

list.Add (com.google.zxing.BarcodeFormat.QR_CODE);

hints = new Hashtable();
hints.Add(com.google.zxing.DecodeHintType.POSSIBLE_FORMATS, list);
hints.Add (com.google.zxing.DecodeHintType.TRY_HARDER, true);
}

if(_multiFormatReader == null)
{
_multiFormatReader = new com.google.zxing.MultiFormatReader();
}

using (var screenImage = CGImage.ScreenImage.WithImageInRect(picFrame))
{
using (_theScreenImage = UIImage.FromImage(screenImage))
{
Bitmap srcbitmap = new System.Drawing.Bitmap(_theScreenImage);
LuminanceSource source = null;
BinaryBitmap bitmap = null;
try {
source = new RGBLuminanceSource(srcbitmap, screenImage.Width, screenImage.Height);
bitmap = new BinaryBitmap(new HybridBinarizer(source));

try {
_multiFormatReader.Hints = hints;
resultb = null;

//_multiFormatReader.decodeWithState(bitmap);

if(resultb != null && resultb.Text!=null)
{

InvokeOnMainThread( () => _parentViewController.BarCodeScanned(resultb));

}
}
catch (ReaderException re)
{
//continue;
}

} catch (Exception ex) {
Console.WriteLine(ex.Message);
}

finally {
if(bitmap!=null)
bitmap = null;

if(source!=null)
source = null;

if(srcbitmap!=null)
{
srcbitmap.Dispose();
srcbitmap = null;
}

}

}
}

return resultb != null;
}

public void StartWorker()
{
if(_thread==null)
{
_thread = new Thread(()=> {

bool result = false;
while (result == false)
{
result = Worker();
Thread.Sleep (67);
}

});

}

_thread.Start();

}

public void StopWorker()
{

if(_thread!=null)
{

_thread.Abort();
_thread = null;

}

//Just in case
_multiFormatReader = null;
hints = null;
}

protected override void Dispose(bool disposing)
{
StopWorker();
base.Dispose(disposing);
}

}
}

有趣的是我看了一下http://blog.reinforce-lab.com/2010/02/monotouchvideocapturinghowto.html尝试看看其他人是如何捕捉和处理视频的,这段代码和我的一样,在大约 40 秒后退出并出现内存警告。

希望 QR 码能在 40 秒内扫描完毕,但我不确定内存是否会被释放,因此问题可能会在扫描许多条码后突然出现。无论哪种方式,都应该可以连续捕获视频源而不会出现内存问题,对吧?

最佳答案

这有点违反直觉,但每次调用 ScreenImage 属性都会创建一个新的 CGImage 实例,因此您还必须对该对象调用 Dispose:

using (var img = CGImage.ScreenImage) {
using (var screenImage = img.WithImageInRect(picFrame))
{
}
}

关于ios - 在使用 Mono Touch 的循环中使用 CGImage.ScreenImage 时出现内存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10818061/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com