Skip to content

Instantly share code, notes, and snippets.

@Eridana
Created March 3, 2016 12:07
Show Gist options
  • Select an option

  • Save Eridana/db6adf7ecf68f40a795a to your computer and use it in GitHub Desktop.

Select an option

Save Eridana/db6adf7ecf68f40a795a to your computer and use it in GitHub Desktop.

Revisions

  1. Eridana created this gist Mar 3, 2016.
    58 changes: 58 additions & 0 deletions Select photo from galleryor take a new
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    - (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];
    }