Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CGPoint from UITapGestureRecognizer

I need to get the x and y position of a UITapGestureRecognizer, but in doing so my application crash

This is the code where I create the Recognizer

-(void)imagePickerController:(UIImagePickerController *) picker didFinishPickingMediaWithInfo:(NSDictionary *) info
{

    [[picker parentViewController] dismissModalViewControllerAnimated:YES];

    UIImage * image =[info objectForKey:@"UIImagePickerControllerOriginalImage"];

    [image drawInRect:CGRectMake(0,0, 200, 400)];

    MyImg =[[UIImageView alloc] initWithImage:image]; 

    UITapGestureRecognizer *recognizer;

    MyImg.userInteractionEnabled=YES;

    recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(getTouchColor:)];
    [MyImg addGestureRecognizer:recognizer];    
    [recognizer release];

    [self.view addSubview:MyImg];

    [picker release];

}

And my event GetTouchColor

-(void)getTouchColor:(UITapGestureRecognizer *) recognizer
{
    if (recognizer.state==UIGestureRecognizerStateEnded)
    {

    CGPoint point = [recognizer locationInView:MyImg];

        NSLog(@"%@", NSStringFromCGPoint(point));
}

If I remove the line

 CGPoint point = [recognizer locationInView:MyImg];

The code works perfectly and the application does not crash.

What am I doing wrong?

Thanks

/ / Sorry my english from google

like image 342
Bruno Leite Avatar asked Jul 25 '11 01:07

Bruno Leite


1 Answers

Check that recognizer is not nil. Sending messages to nil usually returns nil or 0, depending on the return type, but when it’a a struct return type, the result is undefined, and may (among other things) cause a crash.

like image 85
Jeff Kelley Avatar answered Sep 21 '22 15:09

Jeff Kelley