1

I have a tab bar controller, where one of the tabs just brings up the camera. Once the user is dont taking pictures, I want them to to go to another tab that lists the photos they've taken so far (that are saved in the app's directory).

This is how I'm dismissing the camera:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
    UIImage *originalImage;

    // Handle a still image capture
    if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0)
        == kCFCompareEqualTo) {

        originalImage = (UIImage *) [info objectForKey:
                                     UIImagePickerControllerOriginalImage];

        NSData* imageData = UIImagePNGRepresentation(originalImage);

        //Save the file to documents directory
        NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                             NSUserDomainMask, YES);
        NSString* documentsDirectory = [paths objectAtIndex:0];
        NSString* filename = [NSString stringWithFormat:@"img_%@.png", [[NSDate date] description]];
        NSString* path = [documentsDirectory stringByAppendingPathComponent:filename];
        [imageData writeToFile:path atomically:YES];
        [newPicturesPaths addObject:path];

        if(hasConnected) [self upload];
        [self dismissViewControllerAnimated:YES completion:^(void) { [self goToPendingView]; }]; // The debugger puts the crash on this line but AFTER goToPendingView has finished!
        cameraOn = NO;
    }
}

And this is how I prepare the table and switch the tab bar controller's selected tab:

- (void)goToPendingView
{
    [self buildPendingList];
    [(PLEListViewController*)[self.viewControllers objectAtIndex:2] setList:pendingList]; //pendingList is an array
    [self setSelectedIndex:2];
} // The debugger comes this far without a crash

The error is...

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSCFConstantString substringToIndex:]: Range or index out of bounds' *** First throw call stack: (0x3a7173e7 0x395a3963 0x3a717307 0x3a945a47 0x9c7a3 0x928b9 0x39a45541 0x39a2a361 0x39a417ff 0x399fd897 0x37f004eb 0x37f0008d 0x37f00fb1 0x37f0099b 0x37f007ad 0x39a0390f 0x3a6ec941 0x3a6eac39 0x3a6eaf93 0x3a65e23d 0x3a65e0c9 0x3799733b 0x39a4e291 0x66049 0x3ae4fb20) libc++abi.dylib: terminate called throwing an exception

Any help pinpointing what is causing this?

EDIT: I forgot to mention this. The reason why I put [self goToPendingView] in the completion block after dismissing the camera was that if I do this...

    if(hasConnected) [self upload];
    [self dismissViewControllerAnimated:YES completion:nil];
    cameraOn = NO;
    [self goToPendingView];

I get stuck on the camera view, with the 'use' button pressed, and nothing happens, which made me think doing this somehow blocked all my threads.

Shinigami
  • 2,123
  • 23
  • 40
  • 1
    I assume, that the crash actually happens in `PLEListViewController` - maybe in `viewDidLoad` or in your table datasource. Do you do something with the image names? E.g. shortening? – SAE Jan 05 '13 at 02:38
  • Nothing with the names. I just build an array of paths of files in my documents directory, which the table view is supposed to show. The same table view code works fine for another tab in the app (with a different array of data). I did find the list view controller's viewDidLoad gets called before the crash. I'm still not sure how to pinpoint what causes it tho - since the trace just comes from UIApplicationMain. – Shinigami Jan 05 '13 at 03:05
  • 1
    Try stepping through `tableView:cellForRowAtIndexPath:` and check, if it crashes there. Maybe add the code of `PLEListViewController` to your question, since it's hard to imagine, what your code does. – SAE Jan 05 '13 at 03:21
  • You were right. The problem was in my custom table view cell. When the exception was thrown, it was coming straight from UIApplicationMain, so it was hard for me to debug. Thanks to [this answer](http://stackoverflow.com/questions/10770648/objective-c-stack-trace) I learned about break on exception, and that pinpointed it. Should I answer this myself? – Shinigami Jan 06 '13 at 03:01
  • Great. Edit your question and mark it closed. If you wish, upvote my comment. – SAE Jan 06 '13 at 13:31

0 Answers0