这里讲的是
使用UIImagePickerController的实现相机相册的调用。需实现
UIImagePickerControllerDelegate,UINavigationControllerDelegate两个协议。
基本实现步骤:
1,初始化UIImagePickerController
2,判断设备是否支持摄像或允许打开相册
3,实现UIImagePickerController协议
4,进行回调
代码:
//
// ViewController.m
// 相机相册OC
//如果想要相册 功能按钮汉化 需在plist文件中修改 Localization native development region value的值为 China
#define Log(log) NSLog(@"Test:%@",log)
#define LLog(c,log) {NSString * str = [[NSString alloc] initWithFormat:@"%@",c]; NSLog(@"%@:%@",str,log);}
#import "ViewController.h"
#import "UIImagePickerController+Judge.h"
#import "AlertViewController.h"
#import <MobileCoreServices/MobileCoreServices.h>
@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>//UIImagePickerController需要实现的协议
///获取图片显示
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
///系统提供的用来获取图片和视频的接口,执行UIImagePickerControllerDelegate,UINavigationControllerDelegate 两个协议的回调方法
@property (strong,nonatomic) UIImagePickerController * pickerImage;
//判断是选取相册照片,还是相机拍照
@property BOOL isAlum;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
初始化UIImagePickerController
self.pickerImage =[[UIImagePickerController alloc] init];
}
//打开相机,需使用真机测试,否则会崩溃
- (IBAction)camera:(id)sender {
///代理判断获取资源类型
self.isAlum = NO;
//先设定sourceType
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
///设置数据来源类型,这里设置为Camera