IOS基础知识记录八(手机相机或者图片库)

来源:互联网 发布:mac打开文件夹很慢 编辑:程序博客网 时间:2024/05/01 00:46
调用手机相机或者手机图像库


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];}
0 0
原创粉丝点击