//原理:调用
#import "ViewController.h"
@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property(nonatomic,weak)UIImageView *photoImage;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *image=[[UIImageView alloc]initWithFrame:self.view.bounds];
image.backgroundColor=[UIColor grayColor];
[self.view addSubview:image];
self.photoImage=image;
[image setUserInteractionEnabled:YES];
}
//点击屏幕进行图片拍摄
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"开启射像头");
UIImagePickerController *picker=[[UIImagePickerController alloc]init];
//设置摄像头类型 摄像头
picker.sourceType=UIImagePickerControllerSourceTypeCamera;
//打开相册
//picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
//设置代理
picker.delegate=self;
//显示控制器
[self presentViewController:picker animated:YES completion:nil];
}
#pragma mark UIImagePickerControllerDelegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//获取图片,
UIImage *image=info[UIImagePickerControllerOriginalImage];
self.photoImage.image=image;
//退出控制器
[picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end