views:

37

answers:

2

In my current application I have allowed the user to submit an image to an image service online. I allow the user to select from either their Photo Album or take a picture with the Camera.

However, I have an issue. If the device that is being used doesn't have a camera and the user selects to take a photo, the application crashes. I need to be able to determine whether or not the device has the ability to use the cameraDevice.

Below is my current code for presenting an UIActionSheet which allows the user to select the different options.

#pragma mark -
#pragma mark UIImagePickerController
- (IBAction)ImagePicker {
UIActionSheet *sheet = [[UIActionSheet alloc] 
                        initWithTitle:@"" delegate:self 
                        cancelButtonTitle:@"Cancel" 
                        destructiveButtonTitle:nil 
                        otherButtonTitles:@"Choose An Existing Photo", @"Take A Photo", nil];
sheet.actionSheetStyle = UIActionSheetStyleDefault;
[sheet showInView:self.view];
[sheet release];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

if (buttonIndex == 0) {
    //Okay the UIImagePickerControllerSourceTypeSavedPhotosAlbum displays the 
    NSLog(@"Album");
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    [self presentModalViewController:picker animated:YES];
    [picker release];

} else if (buttonIndex == 1) {
    NSLog(@"Camera");
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    [self presentModalViewController:picker animated:YES];
    [picker release];
}
}

Thanks in advance!

+2  A: 
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { NSLog(@"No camera!"); }
muffix
A: 

I won't forget, I had to add tags and a new property, a UIActionSheet called sheet. The following code is how I got everything to work, absolutely seamlessly.

#pragma mark -
#pragma mark UIImagePickerController
- (IBAction)ImagePicker {

if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
sheet = [[UIActionSheet alloc] 
                        initWithTitle:@"" delegate:self 
                        cancelButtonTitle:@"Cancel" 
                        destructiveButtonTitle:nil 
                        otherButtonTitles:@"Choose An Existing Photo", nil];
sheet.actionSheetStyle = UIActionSheetStyleDefault;
[sheet showInView:self.view];
sheet.tag = 0;
[sheet release];
}

else {
    sheet = [[UIActionSheet alloc] 
                            initWithTitle:@"" delegate:self 
                            cancelButtonTitle:@"Cancel" 
                            destructiveButtonTitle:nil 
                            otherButtonTitles:@"Choose An Existing Photo", @"Take A Photo", nil];
    sheet.actionSheetStyle = UIActionSheetStyleDefault;
    [sheet showInView:self.view];
    sheet.tag = 1;
    [sheet release];
}

}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

switch (sheet.tag) {
    case 0:
        if (buttonIndex == 0) {
            //Okay the UIImagePickerControllerSourceTypeSavedPhotosAlbum displays the 
            NSLog(@"Album");
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.delegate = self;
            picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
            [self presentModalViewController:picker animated:YES];
            [picker release];

        }
        break;
    case 1:
        if (buttonIndex == 0) {
            //Okay the UIImagePickerControllerSourceTypeSavedPhotosAlbum displays the 
            NSLog(@"Album");
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.delegate = self;
            picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
            [self presentModalViewController:picker animated:YES];
            [picker release];

        } else if (buttonIndex == 1) {
            NSLog(@"Camera");
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.delegate = self;
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            [self presentModalViewController:picker animated:YES];
            [picker release];
        }
        break;
}
}
the0rkus