Skip to content

Instantly share code, notes, and snippets.

@Eridana
Created March 3, 2016 12:07
Show Gist options
  • Save Eridana/db6adf7ecf68f40a795a to your computer and use it in GitHub Desktop.
Save Eridana/db6adf7ecf68f40a795a to your computer and use it in GitHub Desktop.
Select photo from galleryor take a new
- (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