我想使用目标 c 将带有其他参数的图像上传到服务器
Posted
技术标签:
【中文标题】我想使用目标 c 将带有其他参数的图像上传到服务器【英文标题】:I want to upload image with other parameters to server using objective c 【发布时间】:2018-06-22 07:46:23 【问题描述】:我是 ios 新手,我遇到了必须将图像上传到服务器的情况。这是在 post 方法中所做的事情。
- (IBAction)submitClicked:(UIButton *)sender
NSDictionary *inputData = [NSDictionary dictionaryWithObjectsAndKeys:myImage,@"coverPic", nil];
NSError *error;
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:nil delegateQueue:nil];
NSURL *url = [NSURL URLWithString:@"http://api.mapartment.in/index.php/events/create"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:70.0];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPMethod:@"POST"];
NSData *jsonInputData = [NSJSONSerialization dataWithJSONObject:inputData options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonInputString = [[NSString alloc] initWithData:jsonInputData encoding:NSUTF8StringEncoding];
[request setHTTPBody:[jsonInputString dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
dispatch_async(dispatch_get_main_queue(), ^
NSDictionary *jsondictcity_name = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
if([[jsondictcity_name valueForKey:@"result"] isEqual:@"true"])
NSLog(@"Success");
else
NSLog(@"Try Again");
[self.view resignFirstResponder];
);
];
[postDataTask resume];
附: _localFilePath 包含 - /Users/appcode/Library/Developer/CoreSimulator/Devices/3C3567B8-03BC-4233-B0BC-97E3899D0AAA/data/Containers/Data/Application/3D574024-42FB-4305-807A-ACC3C128383F/Documents/png
【问题讨论】:
我认为您首先需要使用路径获取图像 在附言中我正在获取图像的路径,并将该路径存储在 _localFilePath (NSString 类型)中并使用键 @"coverPic" 传递它 【参考方案1】:图像不会与其他参数一起传递,您必须将图像转换为NSData
UIImage *yourImage= [UIImage imageNamed:@"image.png"];
NSData *imageData = UIImagePNGRepresentation(yourImage);
NSString *postLength = [NSString stringWithFormat:@"%d", [imageData length]];
然后在请求中传递这些数据
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:imageData];
和其他参数作为json传递。
从路径获取 UIImage 使用
UIImage *yourImage = [UIImage imageWithContentsOfFile: imageFilePath];
【讨论】:
我明白了这个概念,但对代码的放置位置感到困惑。所以你能帮我用你建议的图片上传方法编辑我上面的代码吗,因为你可以看到我有一个名为-coverPic的键。感谢您的回复【参考方案2】:- (IBAction)submitClicked:(UIButton *)sender
UIImage* image = [UIImage imageWithContentsOfFile:_localFilePath];
NSString *myImage = [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
NSDictionary *inputData = [NSDictionary dictionaryWithObjectsAndKeys:myImage,@"coverPic",@"POEM WRITING",@"title",@"Bring your own paper",@"desc", nil];
NSError *error;
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:nil delegateQueue:nil];
NSURL *url = [NSURL URLWithString:@"http://api.mapartment.in/index.php/events/create"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:70.0];
[request addValue:@"multipart/form-data" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPMethod:@"POST"];
NSData *jsonInputData = [NSJSONSerialization dataWithJSONObject:inputData options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonInputString = [[NSString alloc] initWithData:jsonInputData encoding:NSUTF8StringEncoding];
[request setHTTPBody:[jsonInputString dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
dispatch_async(dispatch_get_main_queue(), ^
NSDictionary *jsondictcity_name = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
if([[jsondictcity_name valueForKey:@"result"] isEqual:@"true"])
NSLog(@"Success");
else
NSLog(@"Try Again");
[self.view resignFirstResponder];
);
];
[postDataTask resume];
【讨论】:
那么用@"coverPic" 键附加什么? 应用崩溃上线 - [NSString stringWithString: _localFilePath] ];出现错误 - 由于未捕获的异常 'NSInvalidArgumentException' 导致应用程序终止,原因:'*** -[NSPlaceholderString initWithString:]: nil argument' BTW 感谢您的支持 好吧,我认为上传正在发生,因为得到的响应是在 20-30 秒之后,但是当我在邮递员上检查 api 时,coverPic 参数显示为零 是否收到任何响应,例如成功或失败? 我正在成功阻止,但是当我检查列表 API 时,我发现 coverPic 参数为空,这意味着图像没有上传。以上是关于我想使用目标 c 将带有其他参数的图像上传到服务器的主要内容,如果未能解决你的问题,请参考以下文章
在 NodeJS/Express 中将带有其他键值参数的图像上传到 postman