从 iPhone 发送图像作为表单数据的一部分
我已经设法使用以下代码将简单的文本数据发送到服务器:
NSMutableString *parameterString = [[NSMutableString alloc] initWithString: @""];
[parameterString appendString:@"name=steve&"];
[parameterString appendString:@"surname=jobs&"];
[parameterString appendString:@"age=55"];
NSURL *url = [NSURL URLWithString:@"http://example.come/script/"];
request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
NSData *parameterData = [parameterString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:parameterData];
通过它,我可以发送文本表单数据。但我怎样才能发送 PNG 图像呢?
I've managed to get simple text data sent to a server using this code:
NSMutableString *parameterString = [[NSMutableString alloc] initWithString: @""];
[parameterString appendString:@"name=steve&"];
[parameterString appendString:@"surname=jobs&"];
[parameterString appendString:@"age=55"];
NSURL *url = [NSURL URLWithString:@"http://example.come/script/"];
request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
NSData *parameterData = [parameterString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:parameterData];
With that I can send text form data. But how can I send PNG images as well?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将图像转换为 NSData 对象,然后对其进行 base64 编码以将其作为参数发送。
Base64 编码示例可在此处找到:如何在 iphone-sdk 上进行 Base64 编码?
You could just convert the image to an NSData object, then base64 encode it to send it as a parameter.
Base64 encoding examples found here: How do I do base64 encoding on iphone-sdk?