- Monotouch memory leak
- static UIImage unpackedImage = new UIImage();
- public static void DrawCustomImage(IntPtr buffer, int width, int height, int bytesPerRow, CGColorSpace colSpace, byte[] rawPixels, ref UIImage unpackedImage)
- {
- using (var pool = new NSAutoreleasePool())
- {
- GCHandle pinnedArray = GCHandle.Alloc(rawPixels, GCHandleType.Pinned);
- IntPtr pointer = pinnedArray.AddrOfPinnedObject();
- // Set a grayscale drawing context using the image buffer
- CGBitmapContext context = new CGBitmapContext(pointer, width, height, 8, bytesPerRow, colSpace, CGImageAlphaInfo.None);
- try
- {
- // Convert the drawing context to an image and set it as the unpacked image
- //using (var pool = new NSAutoreleasePool())
- {
- using (var img = context.ToImage())
- {
- unpackedImage = UIImage.FromImage(img);
- }
- }
- } finally
- {
- pinnedArray.Free();
- if (context != null)
- context.Dispose();
- }
- }
- GC.Collect();
- }
- SetImages(labelText, symbolArray[0], unpackedImage, points);
- public static void SetImages(String labelText, UIImage symbol, UIImage imageToSet, PointF[] points)
- {
- appReference.InvokeOnMainThread(delegate
- {
- int imageWidth = 716;
- int imageHeight = (int)imageToSet.Size.Height;
- int nextFreeMainImageColumn = 5; // This gets set dynamically, but is simplified here for readability
- lock (displayLocker)
- {
- // Get the current doppler image
- UIImage mainImage = GetMainImage();
- // Add the new imageToSet to the current image by overlaying it adjacent to the current image
- UIImage overlayedImage = overlayImage(mainImage, imageToSet,
- new RectangleF(0, 0, imageWidth, imageHeight),
- new RectangleF(nextFreeMainImageColumn, 0, imageToSet.Size.Width, imageHeight));
- // Update the live screen with the updated image and frame number
- LiveCont.UpdateLiveScreen(labelText, symbol, overlayedImage, points);
- }
- });
- }
- public static UIImage overlayImage(UIImage image, UIImage overlay, RectangleF imageBoundingBox, RectangleF overlayBoundingBox)
- {
- int numBytes = 4; // Four bytes per pixel for a color image (Alpha, Red, Green, Blue)
- int bytesPerRow = (int)imageBoundingBox.Width * numBytes;
- // Set a color drawing context
- CGBitmapContext context = new CGBitmapContext(
- IntPtr.Zero,
- (int)imageBoundingBox.Width,
- (int)imageBoundingBox.Height,
- 8,
- bytesPerRow,
- CGColorSpace.CreateDeviceRGB(),
- CGImageAlphaInfo.NoneSkipFirst
- );
- UIImage overlayedImage = null;
- try
- {
- context.DrawImage(imageBoundingBox, image.CGImage); // Draw the main image
- context.DrawImage(overlayBoundingBox, overlay.CGImage); // Draw the overlay
- using (var img = context.ToImage())
- {
- overlayedImage = UIImage.FromImage(img); // Convert the context back to an image
- }
- }
- finally
- {
- if (context != null)
- context.Dispose();
- image.Dispose();
- }
- return overlayedImage;
- }