美文网首页程序员
ios-上传头像并裁剪成正方形

ios-上传头像并裁剪成正方形

作者: biubiuboom | 来源:发表于2020-05-11 12:57 被阅读0次
    -(void) selectWayToGetPicture{
    
    UIActionSheet*actionSheet = [[UIActionSheetalloc]initWithTitle:nildelegate:selfcancelButtonTitle:@"取消"destructiveButtonTitle:nilotherButtonTitles:@"拍照",@"图库",nilnil];
    
    [actionSheetshowInView:self.view];
    
    }
    
    // 实现UIActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate
    
    #pragma mark -actionSheetDelegate
    
    -(void)actionSheet:(UIActionSheet*)actionSheetclickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if(buttonIndex ==0) {
    
    // 资源类型为照相机
    
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    
    // 判断是否有相机
    
    if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
    
    UIImagePickerController*picker = [[UIImagePickerControlleralloc]init];
    
    picker.delegate=self;
    
    picker.sourceType= sourceType;// 资源类型为照相机
    
    picker.allowsEditing=YES;// 设置选择后的图片是否能被编辑
    
    [selfpresentViewController:pickeranimated:YEScompletion:nil];
    
    }else{
    
    UIAlertView*alertView = [[UIAlertViewalloc]initWithTitle:nilmessage:@"该设备无摄像头"delegate:selfcancelButtonTitle:@"取消"otherButtonTitles:nilnil];
    
    [alertViewshow];
    
    }
    
    }elseif(buttonIndex ==1){
    
    UIImagePickerController*pickerController = [[UIImagePickerControlleralloc]init];
    
    pickerController.sourceType= UIImagePickerControllerSourceTypePhotoLibrary;
    
    pickerController.delegate=self;
    
    pickerController.allowsEditing=YES;// 设置选择后的图片是否能被编辑
    
    [selfpresentViewController:pickerControlleranimated:YEScompletion:nil];
    
    }
    
    }
    
    -(void)imagePickerController:
    
    (UIImagePickerController*)pickerdidFinishPickingMediaWithInfo:(NSDictionary*)info {
    
    NSString*type = [infoobjectForKey:UIImagePickerControllerMediaType];
    
    // 当选择的类型是图片
    
    if([typeisEqualToString:@"public.image"])
    
    {
    
    //需要改成UIImagePickerControllerEditedImage
    
    UIImage* image = [infoobjectForKey:@"UIImagePickerControllerEditedImage"];// 裁剪后的图片
    
    }
    
    [pickerdismissViewControllerAnimated:YEScompletion:nil];
    
    }
    

    相关文章

      网友评论

        本文标题:ios-上传头像并裁剪成正方形

        本文链接:https://www.haomeiwen.com/subject/qhzdnhtx.html