调取相机

阅读: 评论:0

调取相机

调取相机

调取相机--------头像拍照


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:nilpreferredStyle:UIAlertControllerStyleActionSheet];UIAlertAction *cameraAction=[UIAlertAction actionWithTitle:@"拍照"style:UIAlertActionStyleDefaulthandler:^(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) {dalPresentationStyle = UIModalPresentationOverCurrentContext;}[self.viewController presentViewController:picker animated:YES completion:^{;}];}else{[self.viewController.view makeToast:@"该设备没有摄像头" duration:2.0f position:@"center"];}}];UIAlertAction *albumAction=[UIAlertAction actionWithTitle:@"从相册中选择"style:UIAlertActionStyleDefaulthandler:^(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:nildelegate:selfcancelButtonTitle:@"取消"destructiveButtonTitle:@"拍照"otherButtonTitles:@"从相册中选择", nil];[sheet showInView:self]; }
}


3、代理方法

<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);}//--------------}



本文发布于:2024-01-27 18:37:28,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/17063518481933.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:相机
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23