#import "ViewController.h"
#import "AFNetworking.h"
@interface ViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (nonatomic, strong)UIButton * imageButton;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"图片上传";
[self createViewAbout];
}
#pragma mark ---创建视图布局---
- (void)createViewAbout
{
self.imageButton = [[UIButton alloc] initWithFrame:CGRectMake(120, 200, 120, 120)];
_imageButton.layer.cornerRadius = 10;
_imageButton.layer.borderColor = [[UIColor colorWithRed:232/255.0 green:232/255.0 blue:232/255.0 alpha:1.0] CGColor];
_imageButton.layer.borderWidth = 1.0;
[self.view addSubview:_imageButton];
UILabel * imageLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 100, 40)];
imageLabel.text = @"上传图片";
imageLabel.textAlignment = UITextAlignmentCenter;
imageLabel.font = [UIFont fontWithName:@"Arial" size:18];
imageLabel.textColor = [UIColor grayColor];
[self.imageButton addSubview:imageLabel];
[self.imageButton addTarget:self action:@selector(clickUpLoadImage:) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark ---点击上传图片---
- (void)clickUpLoadImage:(UIButton *)button
{
NSLog(@"上传头像");
UIAlertController * alertVC = [UIAlertController alertControllerWithTitle:@"上传头像" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction * alBum = [UIAlertAction actionWithTitle:@"从相册中选取" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:picker.sourceType];
picker.allowsEditing = YES;
picker.delegate = self;
[self presentViewController:picker animated:YES completion:nil];
}];
UIAlertAction * camera = [UIAlertAction actionWithTitle:@"相机拍摄" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
UIImagePickerController * picker1 = [[UIImagePickerController alloc] init];
picker1.sourceType = UIImagePickerControllerSourceTypeCamera;
picker1.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:picker1.sourceType];
picker1.allowsEditing = YES;
picker1.delegate = self;
[self presentViewController:picker1 animated:YES completion:nil];
}];
UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alertVC addAction:cancel];
[alertVC addAction:alBum];
[alertVC addAction:camera];
[self presentViewController:alertVC animated:YES completion:nil];
}
#pragma mark ----实现UIImagePickerControllerDelegate中的协议方法-----
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
[self dismissViewControllerAnimated:YES completion:^{
UIImage * loadImage = info[UIImagePickerControllerOriginalImage];
NSString * url = [NSString stringWithFormat:@"这里填写上传图片网址"];
AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
[manager POST:url parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
formatter.dateFormat = @"yyyyMMddHHmmss";
NSString *fileName = [formatter stringFromDate:[NSDate date]];
[formData appendPartWithFileData:UIImageJPEGRepresentation(loadImage, 0.3) name:@"imageFile" fileName:@"fileName.png" mimeType:@"image/png"];
} success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) {
NSLog(@"头像上传成功反馈== %@", responseObject);
[self.imageButton setBackgroundImage:loadImage forState:UIControlStateNormal];
} failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
NSLog(@"错误信息=====%@", error);
}];
}];
}
@end