4 Replies Latest reply: Sep 26, 2008 6:24 AM by beppethebresh
beppethebresh Level 1 Level 1 (0 points)
I'm trying to code a drawing app for iphone that uses the cgcontextaddlines method that requires a cgpoint array . Since I cannot draw outside of drawrect, I need to define that array externally to drawrect so I need the cgpoint array to be mutable. Since cgpoint is a struct, I need to use c arrays avoiding nsmutablearray, I want then to use dynamic c arrays with the "new" c++ method such is declared in new.h. I've not idea how to include that header...and I don't even know if this method will work.

Anyone has an idea of how to manage this situation?

iPhone 2.1, MBP, Mac OS X (10.5.5)
  • beppethebresh Level 1 Level 1 (0 points)
  • beppethebresh Level 1 Level 1 (0 points)
    I'm sorry if I'm being aggressive but I would really appreciate your help with this problem..
  • just.do.it Level 3 Level 3 (850 points)
    What about keeping the data for your points in a different datastructure wich can be handled in an NSMutableArray.
    For example you can convert a CGPoint into a Dictionary and vice versa with:
    CGPointCreateDictionaryRepresentation and CGPointMakeWithDictionaryRepresentation

    In the drawRecht-method you allocate the cgpointarray with exactly the size you need.

    CGPoint *pointarry;

    numberofitems = <calc the current number of Points needed to be drawn>
    pointarry = alloc(numberofitems * sizeof(CGPoint));
    // Fill array...
    // something like:
    for (index = 0; index < numberofitems; index++) {
  • beppethebresh Level 1 Level 1 (0 points)
    Thank you!!

    Ok, it seems i'm close to the solution, everything works but the allocation of the array.

    This gives a bunch of errors, it seems the "alloc" is not a valid method:

    pointarry = alloc(numberofitems * sizeof(CGPoint));

    I'm able to make it work but since the code is inside the action of a button, everytime I click it, the re-allocation eliminates the previously stored values:

    CGPoint fineSegmento_tasto[100] = {CGPointMake(0.0,0.0),};

    //fineSegmento = alloc(numeroDiLinea * sizeof(CGPoint)); <--not working

    CFDictionaryRef dizionario = CGPointCreateDictionaryRepresentation(segmentoAttuale);

    CGPointMakeWithDictionaryRepresentation(dizionario, &fineSegmento_tasto[numeroDiLinea]);

    fineSegmento = fineSegmento_tasto;

    NSLog(@"Punto 0: %f-%f _ Punto 1: %f-%f _ Punto 2: %f-%f", fineSegmento[0].x, fineSegmento[0].y,fineSegmento[1].x,fineSegmento[1].y,fineSegmento[2].x,fineSegmento[2].y);

    [self setNeedsDisplay];

    And if I try to initialize the cgpointarray outside of the button action code above (at the init of the class), it builds but then crashes when I press the button.

    Do you have any idea of how to initialize that array?

    By the way, i would like to init the array without a limit in numbers of points, so i would not like to declare the dimension of the array as a direct INT.