ios - 通过POST方法发送带有其他参数的图像

时间:2015-09-24 04:38:18

标签: ios objective-c image-uploading

我已经编写了一个代码,用户在网上添加项目并且工作正常..现在我要添加另一个参数即图像..我无法这样做..我的代码是

NSString *posturslString= [NSString stringWithFormat:@"ToDo_Item=%@&ToDo_Date=%@&AddedBy=%i",self.ToDo_Item, [dateformatter stringFromDate:self.ToDo_Date],self.AddedBy];
NSString *postData = [[NSString alloc] initWithString:posturslString];


[request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];


[request setHTTPBody:[postData dataUsingEncoding:NSUTF8StringEncoding]];


NSURLResponse *response = [[NSURLResponse alloc] init];

self.receivedData =[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];

NSError * error;
NSDictionary *user = [NSJSONSerialization JSONObjectWithData:self.receivedData
                                                     options:0
                                                       error:&error];

NSNumber *successNumber =  [user objectForKey:@"success"];
success = [successNumber integerValue];

我如何发送图像,即nsdata

NSData *png=UIImagePNGRepresentation(self.itemImage.image);

以及我的其他参数。请帮助。

1 个答案:

答案 0 :(得分:1)

base64Encoded

中转换图片
NSData *imageData = UIImageJPEGRepresentation(uploadImage, 1.0);
NSString *base64String = [imageData base64EncodedStringWithOptions:kNilOptions];
NSString *encodedString2 = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes( NULL,  (CFStringRef)base64String,    NULL,   CFSTR("!*'();:@&=+$,/?%#[]\" "),   kCFStringEncodingUTF8));

添加与其他关键字相同的图片

NSString *posturslString= [NSString stringWithFormat:@"ToDo_Item=%@&ToDo_Date=%@&AddedBy=%i&image=%@",self.ToDo_Item, [dateformatter stringFromDate:self.ToDo_Date],self.AddedBy,encodedString2];

注意: - 还需要在服务器端实现图像 base64Encoded

的转换