how to get back a CGRect from an NSValue which is stored in a string

2.6k views Asked by At
CGRect rect = Temp_3_Artwork_Big_Image.frame;
NSLog(@"Rect : %.2f,%.2f,%.2f,%.2f",rect.origin.x,rect.origin.y,rect.size.width,rect.size.height);
NSValue *value = [NSValue valueWithCGRect:rect];
NSLog(@"Value : %@",value);
CGRect rect1 = [value CGRectValue];
NSLog(@"rect1 : %.2f,%.2f,%.2f,%.2f",rect1.origin.x,rect1.origin.y,rect1.size.width,rect1.size.height);

Output like

Rect : -0.00,-0.00,829.63,1241.93
Value : NSRect: {{-1.1322711e-05, -2.9491075e-05}, {829.63159, 1241.933}}
rect1 : -0.00,-0.00,829.63,1241.93

I send an NSValue to the server using parsing. That works great and when I get it back from server it is an NSString like

NSRect: {{-1.1322711e-05, -2.9491075e-05}, {829.63159, 1241.933}}

Now how I can convert that back to a CGRect?

1

There are 1 answers

0
Saurabh Passolia On BEST ANSWER

before converting it to NSValue, just log it and see it matches the format in which you have sent it.

you can alternatively use:

NSString *frameString = NSStringFromCGRect(self.window.frame);
CGRectFromString(frameString);

which will work fine and is cleaner.