Created
May 16, 2015 14:54
-
-
Save hashmaparraylist/2477e8ab784b6225e644 to your computer and use it in GitHub Desktop.
从相机或是相册选取照片
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #pragma mark - UIActionSheetDelegate | |
| - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { | |
| if (buttonIndex == 0) { | |
| [self takePhoto]; | |
| } else if (buttonIndex == 1) { | |
| [self choosePhotoFromLibrary]; | |
| } | |
| } | |
| #pragma mark - Private Method | |
| // Call this method in button's action | |
| // Remember add UIActionSheetDelegate to the class extension | |
| - (void)showPhotoMenu { | |
| if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { | |
| UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil | |
| delegate:self | |
| cancelButtonTitle:@"Cancel" | |
| destructiveButtonTitle:nil | |
| otherButtonTitles:@"Take Photo", @"Choose From Library", nil]; | |
| [actionSheet showInView:self.view]; | |
| } else { | |
| [self choosePhotoFromLibrary]; | |
| } | |
| } | |
| // Take photo by camera | |
| - (void)takePhoto { | |
| UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; | |
| imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; | |
| imagePicker.delegate = self; | |
| imagePicker.allowsEditing = YES; | |
| [self presentViewController:imagePicker animated:YES completion:nil]; | |
| } | |
| // choose photo from library | |
| - (void)choosePhotoFromLibrary { | |
| UIImagePickerController *imageController = [[UIImagePickerController alloc] init]; | |
| imageController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; | |
| imageController.delegate = self; | |
| imageController.allowsEditing = YES; | |
| [self presentViewController:imageController animated:YES completion:nil]; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment