UIBezierPath in NSDictionary - is it possible?
I am trying to save a UIBezierPath and some other values in an NSDictionary.
I write in the dictionary like this:
NSMutableArray *paths = [[NSMutableArray alloc]init];
touchesBegan:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint touchPoint = [[touches anyObject] locationInView:self.drawImage];
path = [[UIBezierPath bezierPath] retain];
path.lineCapStyle = kCGLineCapRound;
path.lineWidth = brushSize;
[path moveToPoint:touchPoint];
[self updateDrawingBoard];
}
touchesEnded:
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchPoint = [[touches anyObject] locationInView:self.drawImage];
[path addLineToPoint:touchPoint];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: path, @"path", r, @"red", g, @"green", b, @"blue", alpha, @"alpha", brushSize, @"size", nil];
[paths addObject:dict];
[dict release];
[path release];
path = nil;
[self updateDrawingBoard];
}
And read it like this one :
- (void) updateDrawingBoard {
UIGraphicsBeginImageContext(self.drawImage.bounds.size);
[[UIColor colorWithRed:r green:g blue:b alpha:alpha] setStroke];
NSLog(@"count: %d", [paths count]);
for ( NSDictionary *dict in paths ) {
NSLog(@"dict: %@", dict);
//Here I get the error
UIBezierPath *p = [dict objectForKey:@"path"];
p.lineWidth = [[dict objectForKey:@"size"]floatValue];
[[UIColor colorWithRed:[[dict objectForKey:@"red"]floatValue]
green:[[dict objectForKey:@"green"]floatValue]
blue:[[dict objectForKey:@"blue"]floatValue]
alpha:[[dict objectForKey:@"alpha"]floatValue]] setStroke];
[p stroke];
}
[path stroke];
self.drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
But I get this error:
[__NSArrayI objectForKey:]: unrecognized selector sent to instance 0x1b0210
Don't think that I am doing something wrong.
Dictionary log:
dict: (
"<UIScrollViewDelayedTouchesBeganGestureRecognizer: 0x1b30a0; state = Poss开发者_JS百科ible; enabled = NO; delaysTouchesBegan = YES; view = <UIScrollView 0x1e0380>; target= <(action=delayed:, target=<UIScrollView 0x1e0380>)>>",
"<UIScrollViewPanGestureRecognizer: 0x1b0b60; state = Possible; enabled = NO; delaysTouchesEnded = NO; view = <UIScrollView 0x1e0380>; target= <(action=handlePan:, target=<UIScrollView 0x1e0380>)>; must-fail = {\n <UIScrollViewPagingSwipeGestureRecognizer: 0x1ec7c0; state = Possible; enabled = NO; view = <UIScrollView 0x1e0380>; target= <(action=_handleSwipe:, target=<UIScrollView 0x1e0380>)>>\n }>",
"<UIScrollViewPagingSwipeGestureRecognizer: 0x1ec7c0; state = Possible; enabled = NO; view = <UIScrollView 0x1e0380>; target= <(action=_handleSwipe:, target=<UIScrollView 0x1e0380>)>; must-fail-for = {\n <UIScrollViewPanGestureRecognizer: 0x1b0b60; state = Possible; enabled = NO; delaysTouchesEnded = NO; view = <UIScrollView 0x1e0380>; target= <(action=handlePan:, target=<UIScrollView 0x1e0380>)>>\n }>"
)
Dictionary without paths:
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat:r], @"red",
[NSNumber numberWithFloat:g], @"green",
[NSNumber numberWithFloat:b], @"blue",
[NSNumber numberWithFloat:alpha], @"alpha",
[NSNumber numberWithFloat:brushSize], @"size", nil];
[paths addObject:dict];
[dict release];
Log output:
count: 0
2011-06-09 10:46:28.813 L3T[913:207] count: 1
2011-06-09 10:46:28.815 L3T[913:207] dict: {
alpha = 1;
blue = 0;
green = 1;
red = 0;
size = 5;
}
2011-06-09 10:46:32.552 L3T[913:207] count: 1
sharedlibrary apply-load-rules all
Current language: auto; currently objective-c
(gdb)
Ends also with a crash.
The code you've posted reads fine. It should work fine too but I think the error lies elsewhere. The log,
dict: (
"<UIScrollViewDelayedTouchesBeganGestureRecognizer: 0x1b30a0; state = Possible; enabled = NO; delaysTouchesBegan = YES; view = <UIScrollView 0x1e0380>; target= <(action=delayed:, target=<UIScrollView 0x1e0380>)>>",
"<UIScrollViewPanGestureRecognizer: 0x1b0b60; state = Possible; enabled = NO; delaysTouchesEnded = NO; view = <UIScrollView 0x1e0380>; target= <(action=handlePan:, target=<UIScrollView 0x1e0380>)>; must-fail = {\n <UIScrollViewPagingSwipeGestureRecognizer: 0x1ec7c0; state = Possible; enabled = NO; view = <UIScrollView 0x1e0380>; target= <(action=_handleSwipe:, target=<UIScrollView 0x1e0380>)>>\n }>",
"<UIScrollViewPagingSwipeGestureRecognizer: 0x1ec7c0; state = Possible; enabled = NO; view = <UIScrollView 0x1e0380>; target= <(action=_handleSwipe:, target=<UIScrollView 0x1e0380>)>; must-fail-for = {\n <UIScrollViewPanGestureRecognizer: 0x1b0b60; state = Possible; enabled = NO; delaysTouchesEnded = NO; view = <UIScrollView 0x1e0380>; target= <(action=handlePan:, target=<UIScrollView 0x1e0380>)>>\n }>"
)
indicates that at least one of the objects pushed into the path
array is an array and not an NSDictionary
object. You're not doing it in the code above so it must be elsewhere. I am somehow guessing that you should search for view.gestureRecognizers
within the code in a form similar to,
[paths addObject:view.gestureRecognizers];
where view
is a scrollview object. You will have to take it off unless there was a reason you did it.
精彩评论