从iPhone发送图像作为表单数据的一部分

时间:2010-11-12 17:46:36

标签: ios objective-c forms nsmutableurlrequest

我设法使用以下代码将简单的文本数据发送到服务器:

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图像呢?

2 个答案:

答案 0 :(得分:1)

您可以将图像转换为NSData对象,然后对其进行base64编码以将其作为参数发送。

此处的Base64编码示例:How do I do base64 encoding on iphone-sdk?

答案 1 :(得分:0)

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request addData:imageData withFileName:@"image.png" andContentType:@"image/png" forKey:@"yourParamKey"];