求知 文章 文库 Lib 视频 iPerson 课程 认证 咨询 工具 讲座 Model Center   Code  
会员   
要资料
 
 
 

iOS教程
iOS (iPhone, iPad)教程
IOS - 快速入门
IOS - 开发环境配置
iOS - Objective-C基础
iOS - 创建第一个
IOS - 动作和插座
iOS - Delegates实例
iOS - UI元素
iOS - Accelerometer(加
IOS - 通用应用程序
IOS - 摄像头管理
iOS - 位置处理
iOS - SQLite 数据库
iOS - 发送电子邮箱
iOS - 音频和视频
IOS - 文件处理
IOS - 访问地图
iOS - 应用程序内购买
iOS - iAd 整合
 
 

IOS - 摄像头管理
871 次浏览
33次  

介绍

摄像头是移动设备的共同特点之一。这是普遍的,我们用相机拍照,并用它在我们的应用程序也很简单。

涉及的步骤

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

输出

现在,当我们运行的应用程序,并单击“显示相机按钮,我们将得到下面的输出。

我们采集图片,如下图所示,我们可以编辑图片,即移动和缩放。


您可以捐助,支持我们的公益事业。

1元 10元 50元





认证码: 验证码,看不清楚?请点击刷新验证码 必填



871 次浏览
33次
 捐助