iOS - 相机管理

  • 简述

    相机是移动设备中的常见功能之一。我们可以用相机拍照并在我们的应用程序中使用它,这也很简单。
  • 相机管理 - 涉及的步骤

    Step 1 - 创建一个简单的 View based application.
    Step 2 - 添加一个 buttonViewController.xib 并为按钮创建 IBAction。
    Step 3 - 添加一个 image view 并创建 IBOutlet 并将其命名为 imageView。
    Step 4 − 更新 ViewController.h 如下 -
    
    #import <UIKit/UIKit.h>
    @interface ViewController : UIViewController<UIImagePickerControllerDelegate> {
       UIImagePickerController *imagePicker;
       IBOutlet UIImageView *imageView;
    }
    - (IBAction)showCamera:(id)sender;
    @end
    
    Step 5 − 更新 ViewController.m 如下 -
    
    #import "ViewController.h"
    @interface ViewController ()
    @end
    @implementation ViewController
    - (void)viewDidLoad {
       [super viewDidLoad];
    }
    - (void)didReceiveMemoryWarning {
       [super didReceiveMemoryWarning];
       // Dispose of any resources that can be recreated.
    }
    - (IBAction)showCamera:(id)sender {
       imagePicker.allowsEditing = YES;
       
       if ([UIImagePickerController isSourceTypeAvailable:
       UIImagePickerControllerSourceTypeCamera]) {
          imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
       } else {
          imagePicker.sourceType = 
          UIImagePickerControllerSourceTypePhotoLibrary;
       }
       [self presentModalViewController:imagePicker animated:YES];
    }
    -(void)imagePickerController:(UIImagePickerController *)picker 
       didFinishPickingMediaWithInfo:(NSDictionary *)info {
          UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
          
          if (image == nil) {
             image = [info objectForKey:UIImagePickerControllerOriginalImage];
          }
       imageView.image = image;
    }
    -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
       [self dismissModalViewControllerAnimated:YES];
    }
    @end
    
  • 输出

    当我们运行应用程序并单击显示相机按钮时,我们将获得以下输出 -
    iOS 教程
    拍照后,我们可以编辑图片,即移动和缩放,如下所示 -
    iOS 教程