#pragma mark 头像点击
-(void) headViewClick:(UIButton *)sender
{
NSString *title = @"选项";
NSString *message = @"";
NSString *cancelButtonTitle = @"取消";
NSString *paiButtonTitle = @"拍摄照片";
NSString *locolButtonTitle = @"选取本地";
__weak LFSetPersonInfoViewController *myCtl = self;
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
}];
UIAlertAction *paiAction = [UIAlertAction actionWithTitle:paiButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[myCtl takePhoto];
}];
UIAlertAction *locolAction = [UIAlertAction actionWithTitle:locolButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[myCtl locolSelect];
}];
[alertController addAction:cancelAction];
[alertController addAction:paiAction];
[alertController addAction:locolAction];
// [self presentViewController:alertController animated:YES completion:nil];
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
{
UIPopoverPresentationController *popPresenter = [alertController popoverPresentationController];
// popPresenter.sourceView = self.view;
// popPresenter.sourceRect = CGRectMake(0, kDeviceHeight, 200, 200);
// popPresenter.permittedArrowDirections = UIPopoverArrowDirectionAny;
popPresenter.sourceView = sender;
popPresenter.sourceRect = sender.bounds;
popPresenter.permittedArrowDirections = UIPopoverArrowDirectionUp;
[self presentViewController:alertController animated:YES completion:nil];
}
else
{
[self presentViewController:alertController animated:YES completion:nil];
}
}
#pragma mark 拍照 相册库
-(void) locolSelect
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
//资源类型为图片库
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
// //设置选择后的图片可被编辑
picker.allowsEditing = YES;
[self presentViewController:picker animated:YES completion:^{
}];
}
//开始拍照
-(void)takePhoto
{
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
//设置拍照后的图片可被编辑
picker.allowsEditing = YES;
picker.sourceType = sourceType;
[self presentViewController:picker animated:YES completion:^{
}];
}else
{
NSLog(@"模拟其中无法打开照相机,请在真机中使用");
}
}
//当选择一张图片后进入这里
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
//当选择的类型是图片
if ([type isEqualToString:@"public.image"])
{
//先把图片转成NSData
UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
此处判断图片方向
UIImageOrientation imageOrientation=image.imageOrientation;
if(imageOrientation!=UIImageOrientationUp)
{
// 原始图片可以根据照相时的角度来显示,但UIImage无法判定,于是出现获取的图片会向左转90度的现象。
// 以下为调整图片角度的部分
UIGraphicsBeginImageContext(image.size);
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// 调整图片角度完毕
}
//把图片转成NSData类型的数据来保存文件
NSData *data;
//判断图片是不是png格式的文件
if (UIImagePNGRepresentation(image)) {
//返回为png图像。
data = UIImagePNGRepresentation(image);
}else {
//返回为JPEG图像。
data = UIImageJPEGRepresentation(image, 0.5);
}
headImgVIew.image = image;
reqData = data;//赋值
//上传图片
long long nursDt = (long long)([[NSDate date] timeIntervalSince1970] * 1000.0); //当前时间
NSString *failName = [NSString stringWithFormat:@"%@%lld.jpg",[NSVerify_Tool unarchiveUserID],nursDt];//用户ID + 当前时间
NSString *reqUrl =[NSString stringWithFormat:kRequestBaseURL,kAddOrUpdateMedias];
[activeHUD show:YES];
[LSUpLoadHelper startUploadFilename:failName fileData:reqData WithUrl:reqUrl Completion:^(NSDictionary *dateInfo) {
NSString *imageNameQuan = [[[dateInfo objectForKey:@"ResponseMsg"] objectForKey:@"data"] objectForKey:@"fileUrl"];
reqImgUrl = imageNameQuan;
[activeHUD hide:YES];
} completionFail:^{
[activeHUD hide:YES];
[self hudView:@"上传头像失败"];
}];
//关闭相册界面
[picker dismissViewControllerAnimated:YES completion:^{
}];
}
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
NSLog(@"您取消了选择图片");
//关闭相册界面
[picker dismissViewControllerAnimated:YES completion:^{
}];
}