2012-07-04 8 views
13

mam kod:CGPoint do NSValue i odwrócić

NSMutableArray *vertices = [[NSMutableArray alloc] init]; 

//Getting mouse coordinates 
loc = [self convertPoint: [event locationInWindow] fromView:self]; 
[vertices addObject:loc]; // Adding coordinates to NSMutableArray 

//Converting from NSMutableArray to GLfloat to work with OpenGL 
int count = [vertices count] * 2; // * 2 for the two coordinates of a loc object 
GLFloat []glVertices = (GLFloat *)malloc(count * sizeof(GLFloat)); 
int currIndex = 0; 
for (YourLocObject *loc in vertices) { 
    glVertices[currIndex++] = loc.x; 
    glVertices[currIndex++] = loc.y;   
} 

loc jest CGPoint, więc trzeba jakoś zmienić z CGPoint do NSValue, aby dodać go do NSMutableArray a potem przekonwertować go z powrotem do CGPoint. Jak to możliwe?

Odpowiedz

20

Czy klasa NSValue ma metody +[valueWithPoint:] i -[CGPointValue]? Czy tego właśnie szukasz?

//Getting mouse coordinates 
NSMutableArray *vertices = [[NSMutableArray alloc] init]; 
CGPoint location = [self convertPoint:event.locationInWindow fromView:self]; 
NSValue *locationValue = [NSValue valueWithPoint:location]; 
[vertices addObject:locationValue]; 

//Converting from NSMutableArray to GLFloat to work with OpenGL 
NSUInteger count = vertices.count * 2; // * 2 for the two coordinates 
GLFloat GLVertices[] = (GLFloat *)malloc(count * sizeof(GLFloat)); 
for (NSUInteger i = 0; i < count; i++) { 
    NSValue *locationValue = [vertices objectAtIndex:i]; 
    CGPoint location = locationValue.CGPointValue; 
    GLVertices[i] = location.x; 
    GLVertices[i] = location.y; 
} 
+0

Próbowałem 'NSValue * cgpointObj = [NSValue valueWithPoint: loc];'. Ale jak przekonwertować z powrotem na CGPoint? – hockeyman

+1

Powinny działać coś takiego jak 'CGPoint loc = cgpointObj.pointValue'. Dodałem zaktualizowany kod. – Stream

+0

Myślę, że nie dodaje żadnych wartości. Jeśli napiszę kod: | 'NSUInteger sum = vertices.count; NSLog (@ "count:% lu", suma); ' Zawsze zapisuje sumę = 0. Dlaczego? Żadne wartości nie zostały dodane? Dlaczego więc nie zostały one dodane? – hockeyman