English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
مقدمة: على الأرجح يعرف الجميع أن معظم التطبيقات تحتوي على ميزة "عندي"، وغالبًا ما تحتوي هذه الميزات على صورة الشخصية للمستخدم وأيضًا يمكن تعديل صورة الشخصية. لذا، يقدم هذا المقال لكم ببساطة كيفية استدعاء الكاميرا أو مجلد الصور في تطوير iOS. للحصول على كاميرا النظام أو مجلد الصور، يجب استخدام فئة UIImagePickerController. دعنا نرى كيف يمكن تحقيق ذلك:
أولاً، يجب اتباع协定的 اثنين لـ UIImagePickerController: <UIImagePickerControllerDelegate, UINavigationControllerDelegate>. لماذا هما协定ان؟ يمكنك الضغط على مفتاح command واختيار delegate لـ UIImagePickerController لترى أن هذا الوكيل يتبع协定ين.
#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 - set navigation - (void)setNavigation { self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(selectPhoto:)]; } #pragma mark - action of navigation item - (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; // الانتقال إلى محكم اختيار الصور لفتح الكاميرا [self presentViewController:imagePicker animated:YES completion:nil]; }] // خيار الألبوم UIAlertAction * photo = [UIAlertAction actionWithTitle:@"الألبوم" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { // عند اختيار الألبوم، ضع خصائصUIImagePickerController imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; // الانتقال إلى محكم اختيار الصور لفتح الألبوم [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
حسنًا! تم تقديم جميع أكواد demo لكم، الخطوة الأخيرة هي تكوين ملف plist، لا تنسوا ذلك، وإلا قد تنهار. يُضيف ملف plist حقل Privacy - Camera Usage Description لاستدعاء الكاميرا وحقل Privacy - Photo Library Usage Description لاستدعاء مكتبة الصور. كل شيء جاهز، فقط يبقى هاتف iPhone للتحقق من الاختبار، والتحقق من الكاميرا يتطلب اختبار الجهاز الحقيقي.
هذا هو نهاية محتوى هذا المقال، نأمل أن يكون قد ساعد في تعلمكم، ونأمل أيضًا أن تدعموا تعليمات الشهادة.
البيان: محتوى هذا المقال تم جمعه من الإنترنت، حقوق الطبع والتأليف مملوكة للكاتب الأصلي، المحتوى تم إضافته من قبل مستخدمي الإنترنت بشكل تلقائي، هذا الموقع لا يمتلك حقوق الملكية، لم يتم تعديل المحتوى بشكل إنساني، ولا يتحمل أي مسؤولية قانونية. إذا وجدت محتوى يشتبه في انتهاك حقوق الطبع والتأليف، فلا تتردد في إرسال بريد إلكتروني إلى: notice#oldtoolbag.com (عند إرسال البريد الإلكتروني، يرجى استبدال # ب @) للإبلاغ، وقدم الدليل على الأدلة، إذا تم التحقق من ذلك، سيتم حذف المحتوى المشبوه فورًا.