1.用base64把图片转化为字符串作为关键字上传
主要是获取到图片,然后转化为字符串上传
//先判断图片是否存在
if (image != nil) {
//图片显示在界面上
// [changeImg setBackgroundImage:image forState:UIControlStateNormal];
//以下是保存文件到沙盒路径下
//把图片转成NSData类型的数据来保存文件
NSData *data;
//判断图片是不是png格式的文件
if (UIImagePNGRepresentation(image)) {
//返回为png图像。
data = UIImagePNGRepresentation(image);
}else {
//返回为JPEG图像。
data = UIImageJPEGRepresentation(image, 1.0);
}
NSString *imageStr = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
}
2.用AFNetworking上传
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
[manager POST:urlStr parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
//先判断图片是否存在
if (_SelectImage == nil) {
return ;
} else {
UIImage *iamge = _SelectImage;
NSData *data = UIImagePNGRepresentation(iamge);
[formData appendPartWithFileData:data name:@"file" fileName:@"file.png" mimeType:@"image/png"];
}
} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"%@", responseObject);
NSDictionary *dic = responseObject;
if ([dic[@"Code"] isEqualToString:@"000000"]) {
[hud hideAnimated:YES];
[Tool showHUDWithString:@"保存资料成功" AndView:self.view];
} else {
[hud hideAnimated:YES];
[Tool showHUDWithString:dic[@"Msg"] AndView:self.view];
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[hud hideAnimated:YES];
[Tool showHUDWithString:@"网络君跑路了。。。" AndView:self.view];
NSLog(@"%@",error);
}];
网友评论