Created
March 3, 2016 12:07
-
-
Save Eridana/db6adf7ecf68f40a795a to your computer and use it in GitHub Desktop.
Select photo from galleryor take a new
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
- (IBAction)addPhotoTap:(id)sender { | |
UIAlertController* alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; | |
UIAlertAction* fromGallery = [UIAlertAction actionWithTitle:@"Загрузить из фотоальбома" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { | |
UIImagePickerController * picker = [[UIImagePickerController alloc] init]; | |
picker.delegate = self; | |
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; | |
[self presentViewController:picker animated:YES completion:nil]; | |
}]; | |
[alertController addAction:fromGallery]; | |
UIAlertAction* fromCamera = [UIAlertAction actionWithTitle:@"Сделать снимок" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { | |
UIImagePickerController * picker = [[UIImagePickerController alloc] init]; | |
picker.delegate = self; | |
picker.sourceType = UIImagePickerControllerSourceTypeCamera; | |
[self presentViewController:picker animated:YES completion:nil]; | |
}]; | |
[alertController addAction:fromCamera]; | |
UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Отмена" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { | |
[alertController dismissViewControllerAnimated:YES completion:nil]; | |
}]; | |
[alertController addAction:cancel]; | |
alertController.popoverPresentationController.sourceView = self.view; | |
alertController.popoverPresentationController.sourceRect = CGRectMake( self.avatarImageView.frame.origin.x + self.avatarImageView.width / 2, self.avatarImageView.frame.origin.y + self.avatarImageView.height / 2, 1.0, 1.0); | |
[self presentViewController:alertController animated:YES completion:nil]; | |
} | |
#pragma mark - UIImagePickerControllerDelegate | |
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info | |
{ | |
UIImage *image = info[UIImagePickerControllerOriginalImage]; | |
[self.avatarImageView setImage:image]; | |
[picker dismissViewControllerAnimated:YES completion:nil]; | |
} | |
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo | |
{ | |
NSLog(@"error while saving image: %@", [error description]); | |
} | |
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker | |
{ | |
[picker dismissViewControllerAnimated:YES completion:nil]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment