1.相机的调用
- 要打开系统的相机要在调用相册的类里循序两个协议,分别是UIImagePickerControllerDelegate 和 UINavigationControllerDelegate。首先要有一个全局的UIImagePickerController变量类,主要是通过重写它的setter方法设置这个类的的成员变量或者是属性值,然后执行这两个协议的回调方法,主要的回调方法有三个,拍照时回调方法,拍照取消是的回调方法,将在下面得到简单的体现。
@interface HHMyMessageViewController () <UITableViewDelegate,UITableViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
/**
* 切换到相机的管理器
*/
@property (nonatomic,strong) UIImagePickerController *imagePicker;
/**
* 照片展示视图
*/
@property (nonatomic,strong) UIImageView *_headImage;
@end
@implementation HHMyMessageViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setMessageOfPersonal];
}
- (void)setMessageOfPersonal {
_headImage = [[UIImageView alloc] init];
UIButton *takeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.view addSubview:takeButton];
[takeButton addTarget:self action:@selector(takeClick:) forControlEvents:UIControlEventTouchDown];
}
/**
* 拍照的回调方法
*
* @param sender 点击按钮
*/
- (void)takeClick:(UIButton *)sender {
[self presentViewController:self.imagePicker animated:YES completion:nil];
}
#pragma mark - UIImagePickerController代理方法
// 完成拍照后的回调方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
NSString *mediaType=[info objectForKey:UIImagePickerControllerMediaType];
// 如果是拍照
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
UIImage *image;
// 如果允许编辑则获得编辑后的照片,否则获取原始照片
if (self.imagePicker.allowsEditing) {
// 获取编辑后的照片
image = [info objectForKey:UIImagePickerControllerEditedImage];
}
else{
// 获取原始照片
image = [info objectForKey:UIImagePickerControllerOriginalImage];
}
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
[def setObject:data1 forKey:@"headimage"];
[def synchronize];
// 显示照片
_headImage.image = [UIImage imageWithData:data1];
if (image) {
self.myImage(image);
}
// 保存到相簿
// UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}
[self dismissViewControllerAnimated:YES completion:nil];
}
/**
* 点击相册取消按钮的回调方法
*
* @param picker 取消按钮
*/
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self.imagePicker dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - 重写imagePicker的geter方法
- (UIImagePickerController *)imagePicker{
if (!_imagePicker) {
_imagePicker = [[UIImagePickerController alloc]init];
// 判断现在可以获得多媒体的方式
if ([UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera]) {
// 设置image picker的来源,这里设置为摄像头
_imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
// 设置使用哪个摄像头,这里默认设置为前置摄像头
_imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
if (self.isVideo) {
_imagePicker.mediaTypes = @[(NSString *)kUTTypeMovie];
_imagePicker.videoQuality = UIImagePickerControllerQualityTypeIFrame1280x720;
// 设置摄像头模式(拍照,录制视频)
_imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;
}
else{
// 设置摄像头模式为照相
_imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
}
}
else {
_imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
// 允许编辑
_imagePicker.allowsEditing=YES;
// 设置代理,检测操作
_imagePicker.delegate=self;
}
return _imagePicker;
}