NSDictionary中的UIBezierPath-可能吗? [英] UIBezierPath in NSDictionary - is it possible?

查看:51
本文介绍了NSDictionary中的UIBezierPath-可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to save a UIBezierPath and some other values in an NSDictionary.

我在字典中这样写:

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

touchesBegan:

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:

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) 

也以崩溃告终.

推荐答案

您发布的代码很好用.它也应该可以正常工作,但我认为错误在其他地方.日志,

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

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

的形式搜索view.gestureRecognizers

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

其中,view是滚动视图对象.除非有原因,否则您必须将其取下.

where view is a scrollview object. You will have to take it off unless there was a reason you did it.

这篇关于NSDictionary中的UIBezierPath-可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆