📅  最后修改于: 2020-12-08 06:20:22             🧑  作者: Mango
相机是移动设备的常见功能之一。我们可以用相机拍照并在我们的应用程序中使用它,这也非常简单。
步骤1-创建一个简单的基于View的应用程序。
步骤2-在ViewController.xib中添加一个按钮,并为该按钮创建IBAction。
步骤3-添加图像视图并创建将其命名为imageView的IBOutlet。
步骤4-更新ViewController.h如下-
#import
@interface ViewController : UIViewController {
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
当我们运行应用程序并单击“显示摄像机”按钮时,我们将获得以下输出-
拍照后,我们可以编辑图片,即如下所示移动和缩放-