`
lihao312
  • 浏览: 479553 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

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

阅读更多
调用手机相机或者手机图像库


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
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics