iOS相机管理


相机是移动设备的常见功能之一。我们可以用相机拍照并在我们的应用程序中使用它也很简单。

相机管理 - 涉及的步骤

第1步 - 创建一个简单的 基于View的应用程序

第2步 - 在 ViewController.xib中 添加一个 按钮 ,并为按钮创建IBAction。

第3步 - 添加 图像视图 并创建IBOutlet,将其命名为imageView。

第4步 - 更新 ViewController.h 如下 -

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UIImagePickerControllerDelegate> {
   UIImagePickerController *imagePicker;
   IBOutlet UIImageView *imageView;
}

- (IBAction)showCamera:(id)sender;
@end

第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

输出

当我们运行应用程序并单击show camera按钮时,我们将获得以下输出 -

iOS教程

一旦我们拍照,我们就可以编辑图片,即移动和缩放,如下所示 -

iOS教程