介绍
摄像头是移动设备的共同特点之一。这是普遍的,我们用相机拍照,并用它在我们的应用程序也很简单。
涉及的步骤
1. 创建一个简单的 View based application
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 |
输出
现在,当我们运行的应用程序,并单击“显示相机按钮,我们将得到下面的输出。
我们采集图片,如下图所示,我们可以编辑图片,即移动和缩放。
|
1006 次浏览 |
37次 |
|
捐助 |
|
|