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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With