CGPoint from UITapGestureRecognizer

I need to get x and y position for UITapGestureRecognizer, but this crashes the application

This is the code where I create 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 GetTouchColor event

 -(void)getTouchColor:(UITapGestureRecognizer *) recognizer { if (recognizer.state==UIGestureRecognizerStateEnded) { CGPoint point = [recognizer locationInView:MyImg]; NSLog(@"%@", NSStringFromCGPoint(point)); } 

If I delete the line

  CGPoint point = [recognizer locationInView:MyImg]; 

The code is working fine and the application is not crashing.

What am I doing wrong?

thanks

// Sorry, my English is from Google

+7
source share
3 answers

Make sure the recognizer not nil . Sending messages to nil usually returns nil or 0, depending on the type of return value, but when ita returns the type of the structure, the result is undefined and may (among other things) cause a failure.

+2
source

Firstly, it seems you missed the installation of frame from MyImg.MyImg .

0
source

I believe that the MyImg variable is declared only in imagePickerController , therefore it is not available in getTouchColor , you need to include a link to this view when calling getTouchColor , otherwise it will not recognize MyImg .

0
source

All Articles