Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert RectangleF[] into NSArray of CGRects?

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);
like image 711
Krumelur Avatar asked Dec 04 '12 11:12

Krumelur


People also ask

How to convert the specified rectanglef to a rectangle in Java?

Converts the specified RectangleF to a Rectangle by rounding the RectangleF values to the nearest integer values. The RectangleF to be converted.

How to save a cgrect as an NSDictionary?

You can save the CGRect into an NSDictionary by calling the ToDictionary () method. You can also get an CGRect out a serialized dictionary by using the TryParse (NSDictionary, CGRect) method. Initializes a CGRect structure from a rectangle and a size parameters.

Does custom rounded rectangle usercontrol redraw controls on it?

Custom Rounded Rectangle Usercontrol does not redraw controls on it. Please help. Why .cpp program does not see the GL/glew.h file?


1 Answers

CoreGraphic's typeCGRect 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.

like image 66
poupou Avatar answered Sep 22 '22 08:09

poupou