Search code examples
iphoneiosuigesturerecognizeruiactionsheetunrecognized-selector

Unrecognized selector Error when saving an image using UIImageWriteToSavedPhotosAlbum


A UILongPressGestureRecognizer is added to my imageView with action handleLongPressOnPhotos. The most related codes is as following:

- (IBAction)handleLongPressOnPhotos:(UIImageView *)sender{
self.imageWillBeSaved = sender;
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Save the photo" otherButtonTitles: @"Go to the Original photo", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheet showInView:self.view]; 
[actionSheet release];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
switch (buttonIndex) {
    case 0:
        UIImageWriteToSavedPhotosAlbum(self.imageWillBeSaved.image, self, @selector(image: didFinishSavingWithError:contextInfo:), nil);

        break;

    default:
        break;
}

}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
if (error != NULL)
{
    // handle error
}
else 
{
    // handle ok status
}
}

When the "save the photo" button on the action sheet is clicked, an error message as:-[UILongPressGestureRecognizer image]: unrecognized selector sent to instance 0x21c2a0 Any problem in the code? Thanks in advance!


Solution

  • Your sender is obviously the UILongPressGestureRecognizer.

    Methods that fire when a gesture recognizer is triggered should look like this

    - (void)nameOfMethodHere:(UIGestureRecognizer *)gestureRecognizer;
    

    The argument is the recognizer, not the image view. If the recognizer is only attached to one view you get it via the view property. Otherwise you can get the location of the long tap and hit test to get the view.