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

Untitled

By: a guest on Aug 10th, 2012  |  syntax: None  |  size: 3.62 KB  |  hits: 13  |  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. Monotouch memory leak
  2. static UIImage unpackedImage = new UIImage();
  3.  
  4.     public static void DrawCustomImage(IntPtr buffer, int width, int height, int bytesPerRow, CGColorSpace colSpace, byte[] rawPixels, ref UIImage unpackedImage)
  5.     {
  6.         using (var pool = new NSAutoreleasePool())
  7.         {
  8.             GCHandle pinnedArray = GCHandle.Alloc(rawPixels, GCHandleType.Pinned);
  9.             IntPtr pointer = pinnedArray.AddrOfPinnedObject();
  10.  
  11.             // Set a grayscale drawing context using the image buffer
  12.             CGBitmapContext context = new CGBitmapContext(pointer, width, height, 8, bytesPerRow, colSpace, CGImageAlphaInfo.None);
  13.  
  14.             try
  15.             {
  16.                 // Convert the drawing context to an image and set it as the unpacked image
  17.                 //using (var pool = new NSAutoreleasePool())
  18.                 {
  19.                     using (var img = context.ToImage())
  20.                     {
  21.                         unpackedImage = UIImage.FromImage(img);
  22.                     }
  23.                 }
  24.             } finally
  25.             {
  26.                 pinnedArray.Free();
  27.                 if (context != null)
  28.                     context.Dispose();
  29.             }
  30.         }
  31.         GC.Collect();
  32.     }
  33.  
  34.     SetImages(labelText, symbolArray[0], unpackedImage, points);
  35.  
  36.     public static void SetImages(String labelText, UIImage symbol, UIImage imageToSet, PointF[] points)
  37.     {
  38.         appReference.InvokeOnMainThread(delegate
  39.         {
  40.             int imageWidth = 716;
  41.             int imageHeight = (int)imageToSet.Size.Height;
  42.                             int nextFreeMainImageColumn = 5; // This gets set dynamically, but is simplified here for readability
  43.  
  44.             lock (displayLocker)
  45.             {
  46.                 // Get the current doppler image
  47.                 UIImage mainImage = GetMainImage();
  48.  
  49.                 // Add the new imageToSet to the current image by overlaying it adjacent to the current image
  50.                 UIImage overlayedImage = overlayImage(mainImage, imageToSet,
  51.                                      new RectangleF(0, 0, imageWidth, imageHeight),
  52.                                      new RectangleF(nextFreeMainImageColumn, 0, imageToSet.Size.Width, imageHeight));
  53.  
  54.                 // Update the live screen with the updated image and frame number
  55.                 LiveCont.UpdateLiveScreen(labelText, symbol, overlayedImage, points);
  56.             }
  57.         });
  58.     }
  59.  
  60.     public static UIImage overlayImage(UIImage image, UIImage overlay, RectangleF imageBoundingBox, RectangleF overlayBoundingBox)
  61.     {
  62.         int numBytes = 4;   // Four bytes per pixel for a color image (Alpha, Red, Green, Blue)
  63.         int bytesPerRow = (int)imageBoundingBox.Width * numBytes;
  64.  
  65.         // Set a color drawing context
  66.         CGBitmapContext context = new CGBitmapContext(
  67.             IntPtr.Zero,
  68.             (int)imageBoundingBox.Width,
  69.             (int)imageBoundingBox.Height,
  70.             8,
  71.             bytesPerRow,
  72.             CGColorSpace.CreateDeviceRGB(),
  73.             CGImageAlphaInfo.NoneSkipFirst
  74.         );
  75.  
  76.         UIImage overlayedImage = null;
  77.         try
  78.         {
  79.             context.DrawImage(imageBoundingBox, image.CGImage);             // Draw the main image
  80.             context.DrawImage(overlayBoundingBox, overlay.CGImage);         // Draw the overlay
  81.  
  82.             using (var img = context.ToImage())
  83.             {
  84.                 overlayedImage = UIImage.FromImage(img);                    // Convert the context back to an image
  85.             }
  86.         }
  87.         finally
  88.         {
  89.             if (context != null)
  90.                 context.Dispose();
  91.             image.Dispose();
  92.         }
  93.  
  94.         return overlayedImage;
  95.     }