Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 15th, 2012  |  syntax: None  |  size: 5.21 KB  |  hits: 38  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Memory issue when using CGImage.ScreenImage in a loop using Mono Touch
  2. using System;
  3. using System.Drawing;
  4. using System.Collections.Generic;
  5. using System.Threading;
  6. using MonoTouch.UIKit;
  7. using MonoTouch.Foundation;
  8. using MonoTouch.CoreGraphics;
  9. using com.google.zxing;
  10. using com.google.zxing.common;
  11. using System.Collections;
  12. using MonoTouch.AudioToolbox;
  13. using iOS_Client.Utilities;
  14.  
  15. namespace iOS_Client.Controllers
  16. {
  17.  
  18.     public class CameraOverLayView : UIView
  19.     {
  20.  
  21.         private Thread _thread;
  22.         private CameraViewController _parentViewController;
  23.         private Hashtable hints;
  24.         private static com.google.zxing.MultiFormatReader _multiFormatReader = null;
  25.         private static RectangleF picFrame = new RectangleF(0, 146, 320, 157);
  26.         private static UIImage _theScreenImage = null;
  27.  
  28.  
  29.         public CameraOverLayView(CameraViewController parentController) : base()
  30.         {
  31.             Initialize();
  32.             _parentViewController = parentController;
  33.         }
  34.  
  35.         private void Initialize()
  36.         {              
  37.  
  38.         }
  39.  
  40.         private bool Worker()
  41.         {
  42.  
  43.  
  44.             Result resultb = null;
  45.  
  46.             if(DeviceHardware.Version == DeviceHardware.HardwareVersion.iPhone4
  47.                || DeviceHardware.Version == DeviceHardware.HardwareVersion.iPhone4S)
  48.             {
  49.                 picFrame = new RectangleF(0, 146*2, 320*2, 157*2);
  50.  
  51.             }
  52.  
  53.                 if(hints==null)
  54.                 {
  55.                     var list = new ArrayList();
  56.  
  57.                     list.Add (com.google.zxing.BarcodeFormat.QR_CODE);
  58.  
  59.                     hints = new Hashtable();
  60.                     hints.Add(com.google.zxing.DecodeHintType.POSSIBLE_FORMATS, list);
  61.                     hints.Add (com.google.zxing.DecodeHintType.TRY_HARDER, true);
  62.                 }
  63.  
  64.                 if(_multiFormatReader == null)
  65.                 {
  66.                     _multiFormatReader = new com.google.zxing.MultiFormatReader();
  67.                 }
  68.  
  69.                 using (var screenImage = CGImage.ScreenImage.WithImageInRect(picFrame))
  70.                 {
  71.                     using (_theScreenImage = UIImage.FromImage(screenImage))
  72.                     {
  73.                         Bitmap srcbitmap = new System.Drawing.Bitmap(_theScreenImage);
  74.                         LuminanceSource source = null;
  75.                         BinaryBitmap bitmap = null;
  76.                         try {
  77.                             source = new RGBLuminanceSource(srcbitmap, screenImage.Width, screenImage.Height);
  78.                             bitmap = new BinaryBitmap(new HybridBinarizer(source));
  79.  
  80.                             try {
  81.                                     _multiFormatReader.Hints = hints;
  82.                                     resultb = null;
  83.  
  84.                                     //_multiFormatReader.decodeWithState(bitmap);
  85.  
  86.                                     if(resultb != null && resultb.Text!=null)
  87.                                     {
  88.  
  89.                                         InvokeOnMainThread( () => _parentViewController.BarCodeScanned(resultb));
  90.  
  91.                                     }
  92.                                 }
  93.                             catch (ReaderException re)
  94.                             {
  95.                                 //continue;
  96.                             }
  97.  
  98.                         } catch (Exception ex) {
  99.                             Console.WriteLine(ex.Message);
  100.                         }
  101.  
  102.                         finally {
  103.                             if(bitmap!=null)
  104.                                 bitmap = null;
  105.  
  106.                              if(source!=null)
  107.                                 source = null;
  108.  
  109.                             if(srcbitmap!=null)
  110.                             {
  111.                                 srcbitmap.Dispose();
  112.                                     srcbitmap = null;
  113.                             }
  114.  
  115.                         }  
  116.  
  117.                     }
  118.                 }
  119.  
  120.             return resultb != null;
  121.         }
  122.  
  123.         public void StartWorker()
  124.         {
  125.             if(_thread==null)
  126.             {
  127.                 _thread = new Thread(()=> {
  128.  
  129.                         bool result = false;
  130.                         while (result == false)
  131.                         {
  132.                             result = Worker();
  133.                             Thread.Sleep (67);
  134.                         }              
  135.  
  136.                 });
  137.  
  138.             }
  139.  
  140.             _thread.Start();            
  141.  
  142.         }
  143.  
  144.         public void StopWorker()
  145.         {
  146.  
  147.             if(_thread!=null)
  148.             {
  149.  
  150.                 _thread.Abort();
  151.                 _thread = null;
  152.  
  153.             }
  154.  
  155.             //Just in case
  156.             _multiFormatReader = null;
  157.             hints = null;
  158.         }
  159.  
  160.         protected override void Dispose(bool disposing)
  161.         {
  162.             StopWorker();  
  163.             base.Dispose(disposing);
  164.         }
  165.  
  166.     }
  167. }
  168.        
  169. using (var img = CGImage.ScreenImage) {
  170.     using (var screenImage = img.WithImageInRect(picFrame))
  171.     {
  172.     }
  173. }
  174.        
  175. using (var pool = new NSAutoreleasePool ())
  176. {
  177.     using (var img = CGImage.ScreenImage)
  178.     {      
  179.         using (var screenImage = img.WithImageInRect(picFrame))
  180.         {
  181.             using (_theScreenImage = UIImage.FromImage(screenImage))
  182.             {
  183.             }
  184.         }
  185.     }
  186. }
  187.  
  188. GC.Collect();