我该如何创建一个应用程序,以启动相机并使用相机拍摄的图像更新图像视图?
试一下这个。
在viewDidLoad方法中,初始化UIImagePickerController,将其属性sourceType分配为UIImagePickerControllerSourceTypeCamera,delegate分配为self。
在您的视图控制器中定义一个按钮,在其单击事件中获取imagepicker的模态视图,例如:
[self presentModalViewController:self.picker animated:YES];
这里picker是UIImagePickerController对象。
接下来,实现UIImagePickerController的didFinishPickingMediaWithInfo代理。在这个代理中,您可以将UIImage分配给字典对象info,然后将图像保存到UIImageView的本地实例(它的ImageToSave如下)。
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
UIImage *img = [info objectForKey:@"UIImagePickerControllerImage"];
ImageToSave.image = img;
}
不要忘记将UIImagePickerControllerDelegate包含在你的主视图控制器的.h文件中。如果你仔细想一下,self.view = picker.cameraOverlayView
只是将一个空的透明视图复制到你自己的视图上!它甚至没有添加到屏幕上,即使添加了也不会起作用...
相反,你需要呈现拾取器控制器:
[self.navigationController presentModalViewController:picker animated:YES];
然后确保实现委托调用(看起来您可能已经设置了委托)