0

Iam new to ios, iam an android developer i have code were iam converting image to byte array and sending to server. here is my code in java,

String sResponse = "";
                String url = webpath+"user_upload.php?";
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                MultipartEntity entity = new MultipartEntity();
                entity.addPart("first_name", new StringBody(f_name));
                entity.addPart("last_name", new StringBody(l_name));
                entity.addPart("user_image_name", new ByteArrayBody(
                        image_byte_array, "image/png", ".png"));

                httpPost.setEntity(entity);
                HttpResponse response = httpClient.execute(httpPost);

                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(
                                response.getEntity().getContent(), "UTF-8"));

can any one help me implement this in ios, have tried many codes they are not working for me.. here is the code in ios

NSData *bytes= UIImageJPEGRepresentation(profilePicImageView.image,0.0);
        NSString *recordPostLength = [NSString stringWithFormat:@"%d", [bytes length]];
        NSMutableString *urlstr = [NSMutableString stringWithFormat:@"%@", WEB_URL];
        [urlstr appendFormat:@"first_name=%@", firstName];
        [urlstr appendFormat:@"&last_name=%@",lastName];
        [urlstr appendFormat:@"&user_image_name=%@",recordPostLength];
        NSMutableURLRequest *recordRequest = [[NSMutableURLRequest alloc] init] ;
        [recordRequest setURL:[NSURL URLWithString:urlstr]];
        [recordRequest setHTTPMethod:@"POST"];
        [recordRequest setValue:recordPostLength forHTTPHeaderField:@"Content-Length"];
        [recordRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        [recordRequest setHTTPBody:bytes];
        NSURLResponse *recordResponse;
        NSError *recordError;
        NSData *recordResponseData = [NSURLConnection sendSynchronousRequest:recordRequest returningResponse:&recordResponse error:&recordError];
        NSString *recordResp = [[NSString alloc]initWithData:recordResponseData encoding:NSUTF8StringEncoding];
        NSLog(@"recordResp:%@", recordResp);
cocos-dev
  • 9
  • 3

0 Answers0