FirstApp,iphone开发学习总结7,相机
首先需要一个Nav,在FirstAppAppDelegate.m里添加:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//...
ImageViewController *imgTab = [[ImageViewController alloc] init];
UINavigationController *navImg = [[UINavigationController alloc] initWithRootViewController:imgTab];//添加
NSArray *tabArray = [[NSArray alloc] initWithObjects:navImg, tableTab, btnTab, textTab, navigation, nil];//修改
[navImg release];//添加
//...
}
{
//...
ImageViewController *imgTab = [[ImageViewController alloc] init];
UINavigationController *navImg = [[UINavigationController alloc] initWithRootViewController:imgTab];//添加
NSArray *tabArray = [[NSArray alloc] initWithObjects:navImg, tableTab, btnTab, textTab, navigation, nil];//修改
[navImg release];//添加
//...
}
进入ImageViewController.h文件,添加委托,并创建imageView变量:
@interface ImageViewController : UIViewController<UIImagePickerControllerDelegate, UINavigationControllerDelegate>{
UIImageView *imageView;
}//UIImagePickerControllerDelegate继承至UINavigationControllerDelegate
UIImageView *imageView;
}//UIImagePickerControllerDelegate继承至UINavigationControllerDelegate
ImageViewController.m的- viewDidLoad略做修改,并添加相机按钮://直接使用UIBarButtonSystemItemCamera了
- (void)viewDidLoad
{
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(50.0, 50.0, 48.0, 48.0)];
imageView.image = [UIImage imageNamed:@"China.gif"];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:imageView];
[imageView release];
UIBarButtonItem *imgRightBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(openCamera:)];
[[self navigationItem] setRightBarButtonItem:imgRightBtn];
}
{
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(50.0, 50.0, 48.0, 48.0)];
imageView.image = [UIImage imageNamed:@"China.gif"];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:imageView];
[imageView release];
UIBarButtonItem *imgRightBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(openCamera:)];
[[self navigationItem] setRightBarButtonItem:imgRightBtn];
}
实现按钮事件:
- (void)openCamera:(id)sender
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];//initWithRootViewController:
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
}else {
[imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
[imagePicker setDelegate:self];
[self presentModalViewController:imagePicker animated:YES];
[imagePicker release];
}
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];//initWithRootViewController:
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
}else {
[imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
[imagePicker setDelegate:self];
[self presentModalViewController:imagePicker animated:YES];
[imagePicker release];
}
此处代码:
isSourceTypeAvailable判定此设备是否支持相机,不然从图片目录获取图片。
UIImagePickerController实例以模态展示,需要presentModalViewController它。
souceType分别代表:Camera(打开相机),PhotoLibrary(打开相册),SavedPhotoAlbums(最近拍摄)
图片选择完成,则触发委托事件:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[imageView setImage:image];
[self dismissModalViewControllerAnimated:YES];
}
{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[imageView setImage:image];
[self dismissModalViewControllerAnimated:YES];
}
如果关闭了界面,则取消操作,触发:
imagePickerControllerDidCancel:
可以工作了,此处的一个问题就是当拍摄图片过大,内存警告回收资源,图片则不会显示。通过保存图片等操作解决,后面说。
求指点!