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

iOS開発-システムカメラとアルバムを使用して写真を取得する例

前書き:多くのアプリケーションには私のモジュールがあり、私のモジュールにはユーザーのアバターなどの情報があり、アバターを変更することもできます。それでは、本日はiOS開発でシステムカメラを使って写真を撮影したり、アルバムから写真を取得する方法について簡単に説明します。システムカメラやアルバムを取得するにはUIImagePickerControllerというクラスを使用します。以下にその実装方法を見てみましょう。

まず、UIImagePickerControllerエージェントの2つのプロトコルに従う必要があります:<UIImagePickerControllerDelegate, UINavigationControllerDelegate>。なぜ2つのプロトコルなのか?コマンドキーを押しながらUIImagePickerControllerのデリゲートをクリックすると、実際にはこのエージェントが2つのプロトコルに従っていることがわかります。

#import "HeaderPhotoViewController.h"
@interface HeaderPhotoViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (nonatomic, strong) UIImageView * imageView;
@end
@implementation HeaderPhotoViewController
- (void)viewDidLoad {
  [super viewDidLoad];
  self.navigationItem.title = @"設定画像";
  self.view.backgroundColor = [UIColor whiteColor];
  [self setNavigation];
  [self addSubviews];
  [self makeConstraintsForUI];
}
#pragma mark - ナビゲーション設定
- (void)setNavigation {
  self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(selectPhoto:)];
}
#pragma mark - ナビゲーションアイテムアクション
- (void)selectPhoto:(UIBarButtonItem *)itemCamera {
  //UIImagePickerControllerオブジェクトを作成し、アゲートと編集可能に設定
  UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];
  imagePicker.editing = YES;
  imagePicker.delegate = self;
  imagePicker.allowsEditing = YES;
  //シートポップアップを作成して、カメラかアルバムを選択するように指示します
  UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"開く方法を選択してください" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  //カメラオプション
  UIAlertAction * camera = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    //カメラを選択したとき、UIImagePickerControllerオブジェクトに関連する属性を設定します
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagePicker.modalPresentationStyle = UIModalPresentationFullScreen;
    imagePicker.mediaTypes = @[(NSString *);kUTTypeImage];
    imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
    //カメラに移動してUIImagePickerControllerコントローラーを弹出します
    [self presentViewController:imagePicker animated:YES completion:nil];
  }]
  //アルバムオプション
  UIAlertAction * photo = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    //アルバムを選択したとき、UIImagePickerControllerオブジェクトに関連する属性を設定します
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    //アルバムに移動してUIImagePickerControllerコントローラーを弹出します
    [self presentViewController:imagePicker animated:YES completion:nil];
  }]
  //キャンセルボタン
  UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    [self dismissViewControllerAnimated:YES completion:nil];
  }]
  //添加各个按钮事件
  [alert addAction:camera];
  [alert addAction:photo];
  [alert addAction:cancel];
  //弹出sheet提示框
  [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];
  //取得した画像
  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のすべてのコードが皆さんに提供されました。最後のステップはplistファイルの設定です。これは忘れないでください、忘れたら落ちます。plistファイルにカメラの呼び出しを含むフィールドPrivacyを追加してください。 - Camera Usage Description とアルバムを呼び出すフィールド:Privacy - Photo Library Usage Description。すべてが準備ができていますが、テスト用の実際のiPhoneが必要です。カメラのテストは実際のデバイスを使用して行う必要があります。

これで本文のすべての内容が終わりました。皆様の学習に役立つことを願っています。また、ナイアラチュートリアルのサポートを多くいただければ幸いです。

声明:本文の内容はインターネットから収集され、著作権者に帰属します。インターネットユーザーが自発的に貢献し、自己でアップロードしました。本サイトは所有権を有しなく、人工編集を行っていません。著作権侵害が疑われる内容を見つけた場合は、以下のメールアドレスにご連絡ください:notice#oldtoolbag.com(メールを送信する際には、#を@に変更してください)で通報し、関連する証拠を提供してください。一旦確認がとりたいとされると、本サイトは即座に侵害が疑われるコンテンツを削除します。

基本教程
おすすめ