- Memory issue when using CGImage.ScreenImage in a loop using Mono Touch
- 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);
- }
- }
- }
- using (var img = CGImage.ScreenImage) {
- using (var screenImage = img.WithImageInRect(picFrame))
- {
- }
- }
- using (var pool = new NSAutoreleasePool ())
- {
- using (var img = CGImage.ScreenImage)
- {
- using (var screenImage = img.WithImageInRect(picFrame))
- {
- using (_theScreenImage = UIImage.FromImage(screenImage))
- {
- }
- }
- }
- }
- GC.Collect();