2014年2月19日 星期三

實作一個拍照的UIActionSheet

#pragma mark - Image capture
- (IBAction)takePicture:(UITapGestureRecognizer*)sender {
UIActionSheet *sheet;
sheet = [[UIActionSheet alloc] initWithTitle:@"Pick Photo"
delegate:self
  cancelButtonTitle:@"Cancel"
  destructiveButtonTitle:nil
  otherButtonTitles:@"Take Photo", @"Choose Photo", nil];
[sheet showInView:self.navigationController.view];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - UIActionSheet Delegate
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
switch (buttonIndex) {
case 0: {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:imagePicker animated:YES completion:nil];
}
}
break;
case 1: {
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:imagePicker animated:YES completion:nil];
}
break;
default:
break;
}

}

沒有留言:

張貼留言