9.30.1. Brief introduction of camera ¶
The camera is one of the common features of mobile devices, we can use the camera to take pictures and call it in the application, and the use of the camera is very simple.
9.30.2. Instance step ¶
1、创建一个简单的View based application
2、在ViewController.xib中添加一个button (按钮),并为该按钮创建IBAction
3、添加一个 image view (图像视图),并创建一个名为imageView的IBOutlet
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
9.30.3. Output ¶
When we run the application and click the Show camera button, we get the following output
As long as you take a picture, you can edit the picture by moving and zooming, as shown below.