调取相机--------头像拍照
1、tap 头像,调用相机
<span style="font-size:18px;"> /**
* 上传头像-tap
*
*/
UITapGestureRecognizer *frontTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(takePhoto:)];
frontTap.numberOfTapsRequired = 1;
frontTap.numberOfTouchesRequired = 1;
frontTap.delegate = self;
self.photoImgView.userInteractionEnabled = YES;
[self.photoImgView addGestureRecognizer:frontTap];</span>
</pre>2、获取头像<p></p><p><pre name="code" class="objc">#pragma mark - 获取头像
- (void)takePhoto:(UITapGestureRecognizer *)tapGesture{
if (iOS8) {
UIAlertController *alertController=
[UIAlertController alertControllerWithTitle:@"请选择方式"
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cameraAction=
[UIAlertAction actionWithTitle:@"拍照"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
//拍照
UIImagePickerControllerSourceType type=UIImagePickerControllerSourceTypeCamera;
UIImagePickerController *picker=[[UIImagePickerController alloc] init];
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
picker.sourceType=type;
picker.delegate=self;
picker.allowsEditing=YES;
//-----ios8 后新特幸
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
self.viewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;
}
[self.viewController presentViewController:picker animated:YES completion:^{
;
}];
}else{
[self.viewController.view makeToast:@"该设备没有摄像头" duration:2.0f position:@"center"];
}
}];
UIAlertAction *albumAction=
[UIAlertAction actionWithTitle:@"从相册中选择"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
UIImagePickerControllerSourceType type=UIImagePickerControllerSourceTypePhotoLibrary;
UIImagePickerController *picker=[[UIImagePickerController alloc] init];
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){
picker.sourceType=type;
picker.delegate=self;
picker.allowsEditing=YES;
[self.viewController presentViewController:picker animated:YES completion:^{
;
}];
}else{
// [self.viewController.view showLoadingWithMessage:@"您的相册不可用" hideAfter:1.5];
}
}];
UIAlertAction *cancelAction=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
// NSLog(@"取消");
}];
[alertController addAction:cameraAction];
[alertController addAction:albumAction];
[alertController addAction:cancelAction];
[self.viewController presentViewController:alertController animated:YES completion:nil];
} else {
UIActionSheet *sheet =
[[UIActionSheet alloc]initWithTitle:nil
delegate:self
cancelButtonTitle:@"取消"
destructiveButtonTitle:@"拍照"
otherButtonTitles:@"从相册中选择", nil];
[sheet showInView:self];
}
}
<UIGestureRecognizerDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate,UIActionSheetDelegate>
#pragma mark - ActionSheetDelegate
<pre name="code" class="objc">#pragma mark - ActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;
if (buttonIndex == 0) {
//调用相机拍照
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self.viewController.navigationController presentViewController:imagePicker animated:YES completion:^{
}];
}else{
[self.viewController.view makeToast:@"该设备没有摄像头" duration:2.0f position:@"center"];
}
}else if (buttonIndex == 1){
//从相册获取图片
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self.viewController.navigationController presentViewController:imagePicker animated:YES completion:^{
}];
}else{
// [self.view showLoadingWithMessage:@"您的相册不可用" hideAfter:1.5];
}
}
}
#pragma mark - UIImagePickerControllerDelegate
#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
//选择图片会调用
[picker dismissViewControllerAnimated:YES completion:nil];
UIImage *origImage = [info objectForKey:UIImagePickerControllerOriginalImage];
//图片压缩,因为原图都是很大的,不必要传原图
// UIImage *scaleImage = [self scaleImage:originImage toScale:0.3];
// UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
// NSData *imageDate = UIImageJPEGRepresentation(origImage, 0.2);
self.photoImgView.image = origImage;
NSData *imageData = UIImageJPEGRepresentation(origImage, 1);
while (imageData.length > 1024*1024){
imageData = UIImageJPEGRepresentation(origImage, 0.3);
}
//--------------
}