[NSInvocation setArgument:atIndex:]: index (2) out of bounds [-1, 1] error with UIImageWriteToSavedPhotosAlbum? RTFM :)

Just got the following error:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSInvocation setArgument:atIndex:]: index (2) out of bounds [-1, 1]'

While using: UIImageWriteToSavedPhotosAlbum

I was passing in a selector that didn't take any arguments along the lines of:

UIImageWriteToSavedPhotosAlbum([UIImage imageWithData:self.bytes], self, @selector(photoSaved), nil);

Which, if I had read the docs, I would know is wrong, since the function has to conform to the form:

- (void) image: (UIImage *) image
    didFinishSavingWithError: (NSError *) error
    contextInfo: (void *) contextInfo;

Not entirely intuitive perhaps (my expectation is that if a method expects a selector, it shouldn't dictate form and otherwise it should use a delegate; but this being a C-style function we don't have that luxury) hence this blog post in case anyone else runs into the cryptic error and is left scratching their heads.

Solution found courtesy of this post in Japanese (yeah, code's universal!) :)

Comments