// 调用系统相册
- (void)cemororAction
{
UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"请选择图片来源" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
//从照相机拍照
UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"照相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
self.pickerViewController = [[UIImagePickerController alloc] init];
self.pickerViewController.delegate = self;//设置UIImagePickerController的代理,同时要遵循UIImagePickerControllerDelegate,UINavigationControllerDelegate协议
self.pickerViewController.allowsEditing = YES;//设置拍照之后图片是否可编辑,如果设置成可编辑的话会在代理方法返回的字典里面多一些键值。PS:如果在调用相机的时候允许照片可编辑,那么用户能编辑的照片的位置并不包括边角。
self.pickerViewController.sourceType = UIImagePickerControllerSourceTypeCamera;//UIImagePicker选择器的数据来源,UIImagePickerControllerSourceTypeCamera说明数据来源于摄像头
[self presentViewController:self.pickerViewController animated:YES completion:nil];
}else{
NSLog(@"哎呀,没有摄像头");
}
}];
//从手机相册选取
UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
self.pickerViewController = [[UIImagePickerController alloc]init];
self.pickerViewController.delegate = self;
self.pickerViewController.allowsEditing = YES;//是否可以对原图进行编辑
//设置图片选择器的数据来源为手机相册
self.pickerViewController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:self.pickerViewController animated:YES completion:nil];
}
else{
NSLog(@"图片库不可用");
}
}];
//取消
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alertC addAction:cameraAction];
[alertC addAction:photoAction];
[alertC addAction:cancelAction];
[self presentViewController:alertC animated:YES completion:nil];
}
#pragma mark - UIImagePickerControllerDelegate
#pragma mark - 拍照/选择图片结束
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// NSLog(@"如果允许编辑%@",info);//picker.allowsEditing= YES允许编辑的时候 字典会多一些键值。
//获取图片
// UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];//原始图片
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];//编辑后的图片
NSLog(@"%@",image);
// self.iconImage = image;
//
// [self.iconBtn setBackgroundImage:image forState:UIControlStateNormal];
//
// [self saveImage:image withName:@"fileupload.png"];
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void) saveImage:(UIImage *)currentImage withName:(NSString *)imageName
{
// // UIImage对象 -> NSData对象
// NSData *imageData = UIImagePNGRepresentation(currentImage);
// // 获取沙盒目录
//
// NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];
// // 将图片写入文件
// [imageData writeToFile:fullPath atomically:NO];
//
// AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
// manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/plain"];
// manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
// manager.requestSerializer = [AFJSONRequestSerializer serializer];
// manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//
// /* 文件上传接口 --- upload.268xue.com/goswf?base=mavendemo¶m=user&fileupload=图片二进制 */
// NSDictionary *dic = @{
// @"base":@"mavendemo",
// @"param":@"user"
// };
// [manager POST:@"http://139.129.5.145:83/goswf" parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
// //在网络开发中,上传文件时,是文件不允许被覆盖的,文件重名,解决方法可以在上传时使用当前的系统事件作为文件名
// NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// // 设置时间格式
// formatter.dateFormat = @"yyyyMMddHHmmss";
// NSString *str = [formatter stringFromDate:[NSDate date]];
// NSString *fileName = [NSString stringWithFormat:@"%@.png", str];
// [formData appendPartWithFileData:imageData name:@"fileupload" fileName:fileName mimeType:@"image/png"];
// } success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject) {
// if (responseObject != nil) {
//
// NSData *reciveData = responseObject;
// NSString *reciveString = [[NSString alloc]initWithData:reciveData encoding:NSUTF8StringEncoding];
// [self changeUserImage:reciveString];
// }
//
// } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
// NSLog(@"Error: %@", error);
// }];
}
- (void)changeUserImage:(NSString *)imageUrlStr
{
// NSLog(@"头像的网址是是%@",imageUrlStr);
// AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
// mgr.responseSerializer = [AFJSONResponseSerializer serializer];
//
// NSDictionary *dic = @{
// @"userId":[[NSUserDefaults standardUserDefaults] objectForKey:@"USERID"],
// @"imageUrl":imageUrlStr
// };
// /*头像修改地址
// http://www.yuningwang.com/app/user/avatar?userId=1&avatar=/upload/eduplatform_268/default/ucbanner/hxht-b.jpg
// */
// [mgr POST:@"http://139.129.5.145:81/api/user/updateProfileImg" parameters:dic success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject) {
//
// if ([responseObject[@"success"] integerValue]) {
// NSLog(@"修改成功");
// //重设头像
// NSLog(@"%@",imageUrlStr);
// // NSString *iconstr = [NSString stringWithFormat:@"%@%@",mobileLogo_url,imageUrlStr];
// // [[NSUserDefaults standardUserDefaults] setObject:iconstr forKey:@"avatar"];
// [[NSUserDefaults standardUserDefaults] synchronize];
// [MBProgressHUD showMBPAlertView:@"头像修改成功" withSecond:0];
// // [[NSNotificationCenter defaultCenter] postNotificationName:@"iconChanged" object:self];
// [self.navigationController popViewControllerAnimated:YES];
//
// //self.block();
//
//
// }
// else {
// NSLog(@"修改失败");
// }
// } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
//
// NSLog(@"Error: %@",error);
// }];
}
#pragma mark - 取消拍照/选择图片
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)cemororAction
{
UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"请选择图片来源" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
//从照相机拍照
UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"照相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
self.pickerViewController = [[UIImagePickerController alloc] init];
self.pickerViewController.delegate = self;//设置UIImagePickerController的代理,同时要遵循UIImagePickerControllerDelegate,UINavigationControllerDelegate协议
self.pickerViewController.allowsEditing = YES;//设置拍照之后图片是否可编辑,如果设置成可编辑的话会在代理方法返回的字典里面多一些键值。PS:如果在调用相机的时候允许照片可编辑,那么用户能编辑的照片的位置并不包括边角。
self.pickerViewController.sourceType = UIImagePickerControllerSourceTypeCamera;//UIImagePicker选择器的数据来源,UIImagePickerControllerSourceTypeCamera说明数据来源于摄像头
[self presentViewController:self.pickerViewController animated:YES completion:nil];
}else{
NSLog(@"哎呀,没有摄像头");
}
}];
//从手机相册选取
UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
self.pickerViewController = [[UIImagePickerController alloc]init];
self.pickerViewController.delegate = self;
self.pickerViewController.allowsEditing = YES;//是否可以对原图进行编辑
//设置图片选择器的数据来源为手机相册
self.pickerViewController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:self.pickerViewController animated:YES completion:nil];
}
else{
NSLog(@"图片库不可用");
}
}];
//取消
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alertC addAction:cameraAction];
[alertC addAction:photoAction];
[alertC addAction:cancelAction];
[self presentViewController:alertC animated:YES completion:nil];
}
#pragma mark - UIImagePickerControllerDelegate
#pragma mark - 拍照/选择图片结束
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// NSLog(@"如果允许编辑%@",info);//picker.allowsEditing= YES允许编辑的时候 字典会多一些键值。
//获取图片
// UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];//原始图片
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];//编辑后的图片
NSLog(@"%@",image);
// self.iconImage = image;
//
// [self.iconBtn setBackgroundImage:image forState:UIControlStateNormal];
//
// [self saveImage:image withName:@"fileupload.png"];
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void) saveImage:(UIImage *)currentImage withName:(NSString *)imageName
{
// // UIImage对象 -> NSData对象
// NSData *imageData = UIImagePNGRepresentation(currentImage);
// // 获取沙盒目录
//
// NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];
// // 将图片写入文件
// [imageData writeToFile:fullPath atomically:NO];
//
// AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
// manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/plain"];
// manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
// manager.requestSerializer = [AFJSONRequestSerializer serializer];
// manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//
// /* 文件上传接口 --- upload.268xue.com/goswf?base=mavendemo¶m=user&fileupload=图片二进制 */
// NSDictionary *dic = @{
// @"base":@"mavendemo",
// @"param":@"user"
// };
// [manager POST:@"http://139.129.5.145:83/goswf" parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
// //在网络开发中,上传文件时,是文件不允许被覆盖的,文件重名,解决方法可以在上传时使用当前的系统事件作为文件名
// NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// // 设置时间格式
// formatter.dateFormat = @"yyyyMMddHHmmss";
// NSString *str = [formatter stringFromDate:[NSDate date]];
// NSString *fileName = [NSString stringWithFormat:@"%@.png", str];
// [formData appendPartWithFileData:imageData name:@"fileupload" fileName:fileName mimeType:@"image/png"];
// } success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject) {
// if (responseObject != nil) {
//
// NSData *reciveData = responseObject;
// NSString *reciveString = [[NSString alloc]initWithData:reciveData encoding:NSUTF8StringEncoding];
// [self changeUserImage:reciveString];
// }
//
// } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
// NSLog(@"Error: %@", error);
// }];
}
- (void)changeUserImage:(NSString *)imageUrlStr
{
// NSLog(@"头像的网址是是%@",imageUrlStr);
// AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
// mgr.responseSerializer = [AFJSONResponseSerializer serializer];
//
// NSDictionary *dic = @{
// @"userId":[[NSUserDefaults standardUserDefaults] objectForKey:@"USERID"],
// @"imageUrl":imageUrlStr
// };
// /*头像修改地址
// http://www.yuningwang.com/app/user/avatar?userId=1&avatar=/upload/eduplatform_268/default/ucbanner/hxht-b.jpg
// */
// [mgr POST:@"http://139.129.5.145:81/api/user/updateProfileImg" parameters:dic success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject) {
//
// if ([responseObject[@"success"] integerValue]) {
// NSLog(@"修改成功");
// //重设头像
// NSLog(@"%@",imageUrlStr);
// // NSString *iconstr = [NSString stringWithFormat:@"%@%@",mobileLogo_url,imageUrlStr];
// // [[NSUserDefaults standardUserDefaults] setObject:iconstr forKey:@"avatar"];
// [[NSUserDefaults standardUserDefaults] synchronize];
// [MBProgressHUD showMBPAlertView:@"头像修改成功" withSecond:0];
// // [[NSNotificationCenter defaultCenter] postNotificationName:@"iconChanged" object:self];
// [self.navigationController popViewControllerAnimated:YES];
//
// //self.block();
//
//
// }
// else {
// NSLog(@"修改失败");
// }
// } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
//
// NSLog(@"Error: %@",error);
// }];
}
#pragma mark - 取消拍照/选择图片
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
}