Created
August 28, 2015 03:18
-
-
Save dutran90/abaea7e7ddeafbd0131f to your computer and use it in GitHub Desktop.
This file contains 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
// Show actionsheet | |
NSString *actionSheetTitle = @"Add Photo"; //Action Sheet Title | |
NSString *other1 = @"Take Photo"; | |
NSString *other2 = @"Choose from Gallery"; | |
NSString *cancelTitle = @"Cancel"; | |
UIActionSheet *actionSheet = [[UIActionSheet alloc] | |
initWithTitle:actionSheetTitle | |
delegate:self | |
cancelButtonTitle:cancelTitle | |
destructiveButtonTitle:nil | |
otherButtonTitles:other1, other2, nil]; | |
[actionSheet showInView:self.view]; | |
#pragma mark - UIActionSheetDelegate | |
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ | |
if (buttonIndex == 0) { | |
[self takePhoto]; | |
}else if (buttonIndex == 1){ | |
[self choosePhotoFromGallery]; | |
} | |
} | |
-(void)choosePhotoFromGallery { | |
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; | |
imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext; | |
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; | |
imagePickerController.delegate = self; | |
[self presentViewController:imagePickerController animated:YES completion:nil]; | |
} | |
-(void)takePhoto { | |
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){ | |
// There is not a camera on this device, so don't show the camera button. | |
NSLog(@"No camera device"); | |
}else{ | |
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; | |
imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext; | |
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera; | |
imagePickerController.delegate = self; | |
[self presentViewController:imagePickerController animated:YES completion:nil]; | |
} | |
} | |
#pragma mark - UIImagePickerControllerDelegate | |
// This method is called when an image has been chosen from the library or taken from the camera. | |
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ | |
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage]; | |
if (image) { | |
[_avatarButton setBackgroundImage:image forState:UIControlStateNormal]; | |
} | |
[self dismissViewControllerAnimated:YES completion:NULL]; | |
} | |
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{ | |
[self dismissViewControllerAnimated:YES completion:NULL]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment