iOS Development Call System Camera and Photo Album to Get Photo Sample

  • 2021-11-10 11:03:09
  • OfStack

I believe everyone knows that most app has my module, and in my module, there are basically information such as user's avatar, and the avatar can be changed. So today, this site gives you a brief introduction to how to call the system camera to take pictures or photo albums in the development of iOS. To get the system camera or photo album, we need to use the UIImagePickerController class. Let's look at how to achieve it:

First, you need to follow two protocols of the UIImagePickerController proxy: < UIImagePickerControllerDelegate, UINavigationControllerDelegate > . Why two agreements? You press command and click delegate of UIImagePickerController to see that the agent actually follows two protocols.


#import "HeaderPhotoViewController.h"

@interface HeaderPhotoViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (nonatomic, strong) UIImageView * imageView;
@end

@implementation HeaderPhotoViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  self.navigationItem.title = @" Set Avatar ";
  self.view.backgroundColor = [UIColor whiteColor];

  [self setNavigation];
  [self addSubviews];
  [self makeConstraintsForUI];
}

#pragma mark - set navigation

- (void)setNavigation {

  self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(selectPhoto:)];
}

#pragma mark - navitation item action

- (void)selectPhoto:(UIBarButtonItem *)itemCamera {

  // Create UIImagePickerController Object, and set the proxy and editable 
  UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];
  imagePicker.editing = YES;
  imagePicker.delegate = self;
  imagePicker.allowsEditing = YES;

  // Create sheet Prompt box, prompting to choose camera or photo album 
  UIAlertController * alert = [UIAlertController alertControllerWithTitle:@" Please choose how to open it " message:nil preferredStyle:UIAlertControllerStyleActionSheet];

  // Camera Options 
  UIAlertAction * camera = [UIAlertAction actionWithTitle:@" Camera " style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

    // When you select a camera, set the UIImagePickerController Object-related properties 
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagePicker.modalPresentationStyle = UIModalPresentationFullScreen;
    imagePicker.mediaTypes = @[(NSString *)kUTTypeImage];
    imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
    // Jump to UIImagePickerController Controller pops up camera 
    [self presentViewController:imagePicker animated:YES completion:nil];
  }];

  // Album Options 
  UIAlertAction * photo = [UIAlertAction actionWithTitle:@" Photo album " style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

    // When you select an album, set the UIImagePickerController Object-related properties 
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    // Jump to UIImagePickerController Controller pops up photo album 
    [self presentViewController:imagePicker animated:YES completion:nil];
  }];

  // Cancel button 
  UIAlertAction * cancel = [UIAlertAction actionWithTitle:@" Cancel " style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

    [self dismissViewControllerAnimated:YES completion:nil];
  }];

  // Add individual button events 
  [alert addAction:camera];
  [alert addAction:photo];
  [alert addAction:cancel];

  // Eject sheet Prompt box 
  [self presentViewController:alert animated:YES completion:nil];
}

#pragma mark - add subviews

- (void)addSubviews {

  [self.view addSubview:self.imageView];
}

#pragma mark - make constraints

- (void)makeConstraintsForUI {

  __weak typeof(self)weakSelf = self;

  [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {

    make.size.mas_equalTo(CGSizeMake(Screen_Width, Screen_Width));
    make.centerX.mas_equalTo(weakSelf.view.mas_centerX);
    make.centerY.mas_equalTo(weakSelf.view.mas_centerY);
  }];
}

#pragma mark - imagePickerController delegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {

  [picker dismissViewControllerAnimated:YES completion:nil];
  // Acquired pictures 
  UIImage * image = [info valueForKey:UIImagePickerControllerEditedImage];
  _imageView.image = image;
}

#pragma mark - setter and getter

- (UIImageView *)imageView {

  if (!_imageView) {

    _imageView = [[UIImageView alloc] init];
    _imageView.backgroundColor = [UIColor greenColor];
    _imageView.contentMode = UIViewContentModeScaleAspectFill;
  }
  return _imageView;
}

@end

OK! demo all the code has been presented to everyone, the last step is to configure plist file, do not forget this, or collapse. In the plist file, add the fields Privacy-Camera Usage Description to call the camera and the fields to call the photo album: Privacy-Photo Library Usage Description. Everything is ready, just one test Apple phone is needed, and the camera test needs to be tested with a real machine.


Related articles: