不积跬步,无以至千里
不积小流,无以成江海
项目中的图片上传肯定是必不可少的内容,以下先将项目中的copy下来,而且仅仅是作为头像单个文件的上传,以后有时间在详细整理下
1. 点击头像按钮
所在的视图控制器需要遵从下<UIImagePickerControllerDelegate>代理
- (void)changeImage:(UIButton *)sender {
//创建常见的下滑提示栏, 注意iOS8 才有这个控件
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:(UIAlertControllerStyleActionSheet)];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) { }];
UIAlertAction *fromPhotoAction = [UIAlertAction actionWithTitle:@"从相册选择" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
imagePicker.allowsEditing = YES;
imagePicker.delegate = self;
[self presentViewController:imagePicker animated:YES completion:nil];
}];
UIAlertAction *fromCameraAction = [UIAlertAction actionWithTitle:@"拍照" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;
imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:imagePicker animated:YES completion:nil];
}];
[alert addAction:cancelAction];
[alert addAction:fromCameraAction];
[alert addAction:fromPhotoAction];
[self presentViewController:alert animated:YES completion:nil];
}
2. 选中图片的处理
代理方法获取图片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YES completion:nil];
//可裁剪
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
//原图
//UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[self performSelector:@selector(saveImage:) withObject:image afterDelay:0.5];
}
3. 保存图片
-(void)saveImage:(UIImage *)image{
//传递的参数
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
[parameters setObject:@""forKey:@"userId"];
//图片压缩处理
NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
//利用AFNetworking上传data
AFHTTPSessionManager * mgr = [AFHTTPSessionManager manager];
mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
[mgr POST:[NSString stringWithFormat:@"%@%@",BaseAPI,@"/app/uploadPhoto"] parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
//上传时使用当前的系统事件作为文件名
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyyMMddHHmmss";
formatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT+0800"];
NSString *str = [formatter stringFromDate:[NSDate date]];
NSString *fileName = [NSString stringWithFormat:@"%@.jpg", str];
//上传图片
[formData appendPartWithFileData:imageData name:@"imgFile" fileName:fileName mimeType:@"image/jpeg"];
} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
//返回结果
NSLOG(@"Success >>>>>>>>>>>>>>>>>%@", responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
}
图片上传的基本功能算是有了