我正在使用NSURLConnection來整合Web服務。發佈圖片使用iphone中的參數
我的代碼如下:
NSURL *url = [NSURL URLWithString:Sign_Up_URL];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
NSString *params = [NSString stringWithFormat:@"vimage=%@&@"vFname=%@&vLname=%@&vEmail=%@&vPassword=%@&vDeviceType=%@&vDeviceToken=%@",image_data,txt_first.text,txt_last.text,txt_email.text,txt_password.text,@"IOS",@"safasf423526dgdsgdsg"];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setValue:header_filed forHTTPHeaderField:@"X_API_KEY"];
[urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
[NSURLConnection sendAsynchronousRequest:urlRequest
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response,
NSData *data,
NSError *connectionError) {
// handle response
[MBProgressHUD hideHUDForView:self.view animated:YES];
NSDictionary *json_response = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSLog(@"%@",json_response);
// [self login_response:json_response];
}];
但我怎麼能上傳圖片用這個參數?
說明這一點。
你將不得不在NSData中轉換你的圖像,然後你必須用你的參數上傳這些數據。 – NiravPatel
@NiravPatel,謝謝你的回覆!我已經嘗試轉換NSdata,但沒有得到成功的結果。 – user2893370
請參閱http://stackoverflow.com/questions/11084403/uploading-image-via-post-in-objective-c解決方案。可能是有用的.. – Vidhyanand