IOS调取系统摄像头以及相册

在IOS上很多应用都需要调用系统的摄像头以及相册的权限,IOS也给我们提供了接口。

UIImagePickerController继承自UINavigationController,所以我们不可以在摄像头调用是添加新的界面,会导致导航控制器出错,但是我们可以获取摄像头界面的所有按钮,对按钮进行一些自定义操作。

注意:

摄像头拍照界面,以及拍照完成重拍还有确认选择等操作都是在一个试图控制器上完成的,我们不要误以为是两个界面。为什么可以确定这件事呢?因为我们可以通过方法在该界面ViewController里面获取到操作过程中遇到的所有按钮。

拍照界面视图层级关系如下:


如果要获取PLCropOverlay,你就要先获取PLCameraView的视图。

下面我们来看例子:


ViewController.h:

<pre name="code" class="objc">#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UINavigationControllerDelegate, UIImagePickerControllerDelegate> 

@end

 

ViewController.m:

<pre name="code" class="objc">//
//  ViewController.m
//  摄像头
//
//  Created by admin on 14-10-18.
//  Copyright (c) 2014年 ___FULLUSERNAME___. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) UIImageView *imageView;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.frame = CGRectMake(80, 80, 160, 160);
    imageView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:imageView];
    self.imageView = imageView;
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(110 , 320, 100, 40);
    [button setTitle:@"打开相册" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(openPics) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button2.frame = CGRectMake(110, 420, 100, 40);
    [button2 setTitle:@"打开相机" forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(openCamera) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button2];

}

// 打开相机
- (void)openCamera {
    // UIImagePickerControllerCameraDeviceRear 后置摄像头
    // UIImagePickerControllerCameraDeviceFront 前置摄像头
    BOOL isCamera = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
    if (!isCamera) {
        NSLog(@"没有摄像头");
        return ;
    }
    
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagePicker.delegate = self;
    // 编辑模式
    imagePicker.allowsEditing = YES;
    
    [self  presentViewController:imagePicker animated:YES completion:^{
    }];
    
    
}


// 打开相册
- (void)openPics {
    
    //实例化照片选择控制器
    UIImagePickerController *imagePickerVC = [[UIImagePickerController alloc]init];
    
    //设置照片源
    [imagePickerVC setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];//图片库
    
    //设置照片是否允许编辑
    [imagePickerVC setAllowsEditing:YES];
    
    //设置代理
    [imagePickerVC setDelegate:self];
    //显示控制器,由当前试图控制器复杂展示照片选择控制器
    [self presentViewController:imagePickerVC animated:YES completion:nil];
    
    
}


// 选中照片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
//照片选择完成
    NSLog(@"%@", info);
    // UIImagePickerControllerOriginalImage 原始图片
    // UIImagePickerControllerEditedImage 编辑后图片
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    [self.imageView setImage:image];
    [self dismissViewControllerAnimated:YES completion:nil];//completion之后是关闭之后做的事情
    
}



// 取消相册
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:nil];
    
}
-(UIView *)findView:(UIView *)aView withName:(NSString *)name{
    Class cl = [aView class];
    NSString *desc = [cl description];
    
    if ([name isEqualToString:desc])
        return aView;
    
    for (NSUInteger i = 0; i < [aView.subviews count]; i++)
    {
        UIView *subView = [aView.subviews objectAtIndex:i];
        subView = [self findView:subView withName:name];
        if (subView)
            return subView;
    }
    return nil;
}

-(void)addSomeElements:(UIViewController *)viewController{
    
    
    UIView *PLCameraView=[self findView:viewController.view withName:@"PLCameraView"];
    UIView *bottomBar=[self findView:PLCameraView withName:@"PLCropOverlayBottomBar"];
    UIImageView *bottomBarImageForSave = [bottomBar.subviews objectAtIndex:0];
    UIButton *retakeButton=[bottomBarImageForSave.subviews objectAtIndex:0];
    [retakeButton setTitle:@"重拍" forState:UIControlStateNormal];  //左下角按钮
    [retakeButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    UIButton *useButton=[bottomBarImageForSave.subviews objectAtIndex:1];
    [useButton setTitle:@"确认选择" forState:UIControlStateNormal];  //右下角按钮
    

    
}
- (void)click:(UIButton *)btn
{
    NSLog(@"**********");
//    [btn setTitle:@"重写" forState:UIControlStateNormal];
}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    
    [self addSomeElements:viewController];
}
@end

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值