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

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


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]; 
    }
}


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

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值