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

-(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 上传头像并裁剪成正方形)