iOS中调用相机以及相册

有时候需要在程序中调用系统的相机以及手机相册,接下来简要介绍调用的方法:
//.h文件
@interface CameraViewController : UIViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (nonatomic,retain)UIImageView *imageView;//
@property (nonatomic,retain)UIImagePickerController *imageController;//图片控制器

@end
</pre><p></p><p>这个时候可以给UIImageView进行初始化,然后初始化两个Button,分别是调用相机和调用相册</p><p></p><p></p><pre name="code" class="objc">    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 64, 200, 200)];
    _imageView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:_imageView];


    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    button1.frame = CGRectMake(20, 400, 80, 50);
    [button1 setTitle:@"照相机" forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(enterCramera:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];

    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
    button2.frame = CGRectMake(150, 400, 100, 50);
    [button2 setTitle:@"图库" forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(enterPhotos:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button2];


实现两个Button的方法


//打开相机
- (void)enterCramera:(UIButton*)button
{
    //如果设备里面没有相机
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        //创建一个图片选择器
        self.imageController = [[UIImagePickerController alloc] init];
        self.imageView = UIImagePickerControllerSourceTypePhotoLibrary;
        _imageController.delegate = self;
        _imageController.allowsEditing = NO;
        [self presentViewController:_imageController animated:YES completion:nil];
        
    }else{
        UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"提示" message:@"该设备不支持相机功能" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        //[self.view addSubview:alertV];
        [alertV show];
    }
    
}


//打开相册
- (void)enterPhotos:(UIButton*)button
{
    //创建一个图片选择器
    self.imageController = [[UIImagePickerController alloc]init];
    //指定图片来源
    _imageController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;//从现有的照片库取照片
    _imageController.delegate = self;
    _imageController.allowsEditing = YES;//设置为yes时,取出图片后会对图片进行放大或缩小以及移动位置
    [self presentViewController:_imageController animated:YES completion:nil];
    
}

这时候就实现了对相机以及相册的调用;


imagePickControllerDelegate代理方法可以帮助我们实现更加丰富的功能

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo NS_DEPRECATED_IOS(2_0, 3_0);
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;


//UINavigationControllerDelegate代理方法
<span style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 20px;">//当一个viewController要显示的时候通知一下外面,给我们一个机会进行设置,包含如下两个函数:</span>
<span style="font-family: Arial, Helvetica, sans-serif;">- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;</span>
<span style="font-family: Arial, Helvetica, sans-serif;">- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated;</span>
<span style="font-family: Arial, Helvetica, sans-serif;">- (NSUInteger)navigationControllerSupportedInterfaceOrientations:(UINavigationController *)navigationController NS_AVAILABLE_IOS(7_0);</span>
<span style="font-family: Arial, Helvetica, sans-serif;">- (UIInterfaceOrientation)navigationControllerPreferredInterfaceOrientationForPresentation:(UINavigationController *)navigationController NS_AVAILABLE_IOS(7_0);</span>
<span style="font-family: Arial, Helvetica, sans-serif;">- (id <UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController</span>

                          interactionControllerForAnimationController:(id <UIViewControllerAnimatedTransitioning>) animationController NS_AVAILABLE_IOS(7_0);

- (id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                   animationControllerForOperation:(UINavigationControllerOperation)operation
                                                fromViewController:(UIViewController *)fromVC
                                                  toViewController:(UIViewController *)toVC  NS_AVAILABLE_IOS(7_0);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值