English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

iOS Development-Example of Calling System Camera and Album to Get Photos

Preface: I believe everyone knows that most apps have my module, and my module basically has user profile pictures and other information, and it is possible to change the avatar. So today, I will give you a brief introduction to how to call the system camera to take photos or how to get photos from the album in iOS development. To access the system camera or album, we need to use the UIImagePickerController class. Let's take a look at how to implement it:

Firstly, it is necessary to follow the two protocols of the UIImagePickerController delegate: <UIImagePickerControllerDelegate, UINavigationControllerDelegate>. Why are there two protocols? By pressing the command key and clicking on the delegate of UIImagePickerController, you will find that this delegate actually conforms to 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 - navigation item action
- (void)selectPhoto:(UIBarButtonItem *)itemCamera {
  //Create an UIImagePickerController object and set the delegate and editable
  UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];
  imagePicker.editing = YES;
  imagePicker.delegate = self;
  imagePicker.allowsEditing = YES;
  //Create a sheet prompt box to prompt the selection of camera or album
  UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"Please select the opening method" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  //Camera options
  UIAlertAction * camera = [UIAlertAction actionWithTitle:@"Camera" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    //Set related properties of the UIImagePickerController object when selecting the camera
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagePicker.modalPresentationStyle = UIModalPresentationFullScreen;
    imagePicker.mediaTypes = @[(NSString *);
    imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
    //Jump to the UIImagePickerController controller to pop up the camera
    [self presentViewController:imagePicker animated:YES completion:nil];
  }]
  //Album options
  UIAlertAction * photo = [UIAlertAction actionWithTitle:@"Album" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    //Set related properties of the UIImagePickerController object when selecting the album
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    //Jump to the UIImagePickerController controller to pop up the 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 various button events
  [alert addAction:camera];
  [alert addAction:photo];
  [alert addAction:cancel];
  //Pop up a 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];
  //obtained image
  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! All the code for demo has been presented to everyone, the final step is to configure the plist file, don't forget this, or it will crash. Add the Privacy field for calling the camera in the plist file - Camera Usage Description and the field of calling the album: Privacy - Photo Library Usage Description. Everything is ready, just a test iPhone is missing, the camera test needs to be done with a real device.

That's all for this article. I hope it will be helpful to everyone's study and I also hope everyone will support the Naying Tutorial more.

Statement: The content of this article is from the Internet, the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, does not edit manually, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the suspected infringing content.)

You May Also Like