NSDictionary中的UIBezierPath - 有可能吗?

时间:2011-06-08 19:40:56

标签: iphone xcode nsdictionary uibezierpath

我正在尝试在NSDictionary中保存UIBezierPath和其他一些值。

我在字典中写道:

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];
}

并像这样阅读:

- (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();
}

但是我收到了这个错误:

[__NSArrayI objectForKey:]: unrecognized selector sent to instance 0x1b0210

不要以为我做错了什么。

字典日志:

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    }>"
  )

没有路径的词典:

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];

日志输出:

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) 

结束了崩溃。

1 个答案:

答案 0 :(得分:1)

您发布的代码读得很好。它应该工作正常,但我认为错误在其他地方。日志,

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    }>"
  )

表示推入path数组的对象中至少有一个是数组,而不是NSDictionary对象。你没有在上面的代码中这样做,所以它必须在其他地方。我在某种程度上猜测你应该在代码中以类似于

的形式搜索view.gestureRecognizers
[paths addObject:view.gestureRecognizers];

其中view是一个scrollview对象。除非你有理由这样做,否则你必须把它取下来。