1.相機的調用
- 要打開系統(tǒng)的相機要在調用相冊的類里循序兩個協(xié)議剿另,分別是UIImagePickerControllerDelegate 和 UINavigationControllerDelegate耳峦。首先要有一個全局的UIImagePickerController變量類,主要是通過重寫它的setter方法設置這個類的的成員變量或者是屬性值才避,然后執(zhí)行這兩個協(xié)議的回調方法,主要的回調方法有三個,拍照時回調方法捺宗,拍照取消是的回調方法对蒲,將在下面得到簡單的體現(xiàn)钩蚊。
@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];
// 判斷現(xiàn)在可以獲得多媒體的方式
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;
}
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者