How to convert RectangleF[] into NSArray of CGRects?

c#, core-graphics, xamarin.ios

Solution

CoreGraphic's type`CGRect` is mapped to System.Drawing's `RectangleF` in MonoTouch. When writing bindings you write the later (in C#) when you see the former (in ObjectiveC).

EDIT However the binding generator cannot convert `NSArray` into arrays of non-`NSObject` (e.g. value types like `RectangleF`). In such case you can bind them as `NSArray` and mark them with `[Internal]` then provide a better (manual) overload for public consumption.

Then you can create your own `Rectangle[]`, based on the size of the `NSArray`, then iterate the `NSArray` elements to get the elements. You'll need to convert (see `NSObject` and `NSValue` helpers) each element to a `RectangleF`.

Also you need to keep `NSArray` when you do not know what types you'll get.

Problem

I'm sure I have done it before but I don't find how to: I have a binding here that expects an `NSArray`. The contents are `CGRect` objects. How do I get an `NSArray` of `CGRect` from an array of `RectangleF[]`? The other thing I have to convert is: `PointF[][]` into an `NSArray of CGPoint[] Note that I'm actually facing a bindings problem here: ``` /// Coordinates for highlight annotation (boxed CGRect's) @property (nonatomic, strong) NSArray *rects; /// Array of lines (which is a array of CGPoint's) @property (nonatomic, copy) NSArray *lines; ``` This is what they currently are: ``` [Export ("rects")] NSArray Rects { get; set; } [Export ("lines")] NSArray Lines { get; set; } ``` This is what won't compile (in the bindings project): ``` [Export ("rects")] RectangleF[] Rects { get; set; } [Export ("lines")] PointF[][] Lines { get; set; } ``` EDIT/SOLUTION: Following PouPou's recommendations, I came up with the following working code. To convert an array of array of PointF into an NSArray, I implemted this method. Note that its input is actually an AnnotationPoint but that gets converted into PointF: ``` /// <summary> /// Extends AnnotationPoint[][] to allow conversion into an NSArray containing arrays of CGPoint. /// </summary> /// <returns>the NSArray</returns> /// <param name='aStrokePath'>the path to convert</param> public static NSArray ToPSPDFInkLines(this AnnotationPoint[][] aStrokePath) { List<NSArray> aLines = new List<NSArray>(); foreach ( AnnotationPoint[] aLine in aStrokePath ) { List<NSObject> aLinePoints = new List<NSObject>(); foreach (AnnotationPoint oPoint in aLine) { aLinePoints.Add(NSObject.FromObject(oPoint.ToPointF())); } var oNSLineArray = NSArray.FromNSObjects(aLinePoints.ToArray()); aLines.Add(oNSLineArray); } NSArray oNSArray = NSArray.FromNSObjects(aLines.ToArray()); return oNSArray; } ``` To convert the RectangleF[] into NSArray, this is what I went for. Note that here, I'm working with AnnotationRegion objects but they get converted into RectangleF: ``` NSObject[] aRects = oHighlightAnnot.Coords.Select(oRegion => NSObject.FromObject(oRegion.ToRectangleF())).ToArray(); NSArray oNSArray = NSArray.FromNSObjects(aRects); ```

Original source