故事背景:我们可以获取到手机系统相册里面的图片,并且可以把它转化成UIImage *,至于本地图片,直接可以根据图片名字把它转化成UIImage * 类型的,其比较简单,在此不做说明。关键问题是,把这样的类型,如何上传到网页?也就是变成一个可以点开的链接。在此,我利用第三方AFNetworking上传。代码如下:
#import "AFNetworking.h"
@interface ViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate> {
UIImageView *_imageView; // 设置一个接受图片的控件
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 创建控件 触发效果
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 100, 100, 100);
button.backgroundColor = [UIColor lightGrayColor];
[button setTitle:@"相册" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
UIImageView *imageView = [[UIImageView alloc] init];
imageView.frame = CGRectMake(200, 300, 200, 200);
[self.view addSubview:imageView];
_imageView = imageView;
}
// 按钮点击事件
- (void)buttonClick {
// 1.判断是否可用
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
return;
}
// 2. 创建图片选择控制器
UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
// 3. 设置打开照片相册类型(显示所有相簿)
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
ipc.delegate = self;
[self presentViewController:ipc animated:YES completion:nil];
}
// 获取图片后的操作
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
// 设置图片
UIImage *image = info[UIImagePickerControllerOriginalImage];
_imageView.image = image;
// 销毁控制器
[picker dismissViewControllerAnimated:YES completion:nil];
// 上传图片的方法
[self uploadImageWithImage:image];
}
// 上传图片的方法
- (void)uploadImageWithImage:(UIImage *)image {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
// 上传图片时候 用到的URLStr 这个是上传图片到网页用到的接口上的链接
NSString *imageUrlStr = @"http://58.51.146.24/jfinalbbs/file/upload_pic";
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json", nil];
[manager POST:imageUrlStr parameters:nil constructingBodyWithBlock:^(id _Nonnull formData) {
NSData *imageData = UIImageJPEGRepresentation(image, 0.1);
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyyMMddHHmmss";
NSString *str = [formatter stringFromDate:[NSDate date]];
NSString *fileName = [NSString stringWithFormat:@"%@.jpg", str];
/ name 就是 我们要传的参数名字(file_upload)
[formData appendPartWithFileData:imageData name:@"file_upload" fileName:fileName mimeType:@"image/jpeg"];
} progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
//NSLog(@"responseObject = %@, task = %@",responseObject,task);
// subUrl 是服务器给我们返回的链接后面的一小部分
NSString *subUrl = responseObject[@"url"];
//最终转化成功的图片链接 要在前面拼接上公司的信息,这个服务器人员说的算
NSString *needURL = [NSString stringWithFormat:@"http://idongtian.com/jfinalbbs%@",subUrl];
// 最后把这个转化成功的链接打印出来,复制到网页上面,是不是可以打开看到上传的图片了,有没有小兴奋呢
NSLog(@"%@",needURL);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"error = %@",error);
}];
}