1.调用手机相机或者图片库要遵循协议
UIImagePickerControllerDelegate UINavigationControllerDelegate//方便隐藏状态栏
2.通过模态显示相机或者图片库
UIImagePickerController *imagePicker; imagePicker = [[UIImagePickerController alloc] init]; if ([camera isOn]) { //前置还是后置摄像头 imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceFront; imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; }else { imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; } imagePicker.delegate = self; [self presentViewController: imagePicker animated:YES completion: nil];
注: 一般相机或者图片库时, 会全屏显示的,最好把状态栏隐藏掉
[[UIApplication sharedApplication] setStatusBarHidden: YES];
说明:
[self presentViewController: imagePicker animated:YES completion: nil]; //该方法是ios6新加的 替代下面方法显示模态 [self presentModalViewController:<#(UIViewController *)#> animated:<#(BOOL)#>]
3.遵循协议实现两个方法
//UIImagePickerControllerDelegate -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { [[UIApplication sharedApplication] setStatusBarHidden: NO]; [self dismissViewControllerAnimated: YES completion: nil]; //here you code } //UIImagePickerControllerDelegate -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { [[UIApplication sharedApplication] setStatusBarHidden:NO]; [self dismissViewControllerAnimated: YES completion: nil]; }