0

UIBezierPath とその他の値を NSDictionary に保存しようとしています。

私は辞書に次のように書いています。

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) 

こちらもクラッシュで終了。

4

1 に答える 1

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

配列にプッシュされたオブジェクトの少なくとも 1 つがオブジェクトではpathなく配列であることを示しNSDictionaryます。上記のコードではそれを行っていないため、他の場所にある必要があります。view.gestureRecognizersどういうわけか、コード内で次のような形式で検索する必要があると推測しています。

[paths addObject:view.gestureRecognizers];

viewスクロールビューオブジェクトです。やむを得ない理由がない限り、脱ぐ必要があります。

于 2011-06-09T04:52:55.870 に答える