Wednesday, 11 May 2011

Camera + Action Sheets Basics

Header should implement:

@interface CapturedPhotoController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIActionSheetDelegate> {


and then we paste these methods into the main file:



- (IBAction) takePictureButton: (id) sender {
NSLog(@"snap");

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
NSLog(@"is camera ok");
UIActionSheet *photoSourceSheet = [[UIActionSheet alloc] initWithTitle:@"Select fugitive picture"
delegate:self
cancelButtonTitle:@"cancel"
destructiveButtonTitle:nil
otherButtonTitles:@"Take new photo", @"Choose existing photo", nil, nil];

[photoSourceSheet showInView:self.view];
[photoSourceSheet release];
}
else {
NSLog(@"No camera");
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
picker.allowsEditing = YES;
[self presentModalViewController:picker animated:YES];
}
}

-(void) actionSheet:(UIActionSheet *) actionShet didDismissWithButtonIndex:(NSInteger)buttonIndex {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;

switch(buttonIndex) {
case 0:
NSLog(@"user wants to take new picture");
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.delegate = self;
picker.allowsEditing= YES;
break;
case 1:
NSLog(@"user wants to choose existing picture");
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
break;
default:
[picker release];
return;
}
[self presentModalViewController:picker animated:YES];

}

-(void) imagePickerController:(UIImagePickerController *) picker
didFinishPickingImage:(UIImage*) image
editingInfo:(NSDictionary*) editingInfo {
self.fugitive.image = UIImagePNGRepresentation(image);
[self dismissModalViewControllerAnimated:YES];
[picker release];
}

1 comment:

  1. Do you know how to make a button at index 0 in the ActionSheet open a new view?

    ReplyDelete