user1878531 user1878531 - 2 years ago 76
iOS Question

Image not posting by base64 string on json

I am Posting a image on Server By json Post Webservice.I have to upload the image on base 64 .I am encoding my image to base 64 string but the Image is not posting on the server and not other things.There is No problem with the webservice.The image is uploading successfully in android.

selectedImage=[[NSData alloc]initWithData:UIImageJPEGRepresentation(image, 1.0)];
[[NSUserDefaults standardUserDefaults]setObject:selectedImage forKey:@"image"];
[[NSUserDefaults standardUserDefaults]synchronize];
strImage=[[NSString alloc]init];
strImage = [selectedImage base64Encoding];
strImage=[strImage stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSLog(@"strImage %@",strImage);
/*
NSData *b64DecData = [Base64 decode:strImage];
NSLog(@"strImage %@",strImage);
[bttnimage setBackgroundImage:[UIImage imageWithData:b64DecData] forState:UIControlStateNormal];
*/


NSString *post=[[NSString alloc]initWithFormat:@"name=%@&aboutMe=%@&chatId=%@&gender=%@&lookingFor=%@&city=%@&birthdate=%@&anniversarydate=%@&number1=%@&number2=%@&number3=%@&image=%@",[txtProfileName.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[txtComment.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[appDelegate.chatid stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[bttnGender.titleLabel.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[bttnLookingfor.titleLabel.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[txtPlace.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[dobLabel.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[anniversaeyLabel.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[txtNumber.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[txtNum1.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[txtNum2.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],strImage];

NSLog(@"post %@",post);
NSData * postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];

NSString * postLength = [NSString stringWithFormat:@"%d",[postData length]];

NSMutableURLRequest *latrequest = [[NSMutableURLRequest alloc] init];

NSString *url=[NSString stringWithFormat:@"http://www.xyzAbc.org/iphone/updateProfile.php?%@",post];

NSLog(@"url %@",url);

[latrequest setURL:[NSURL URLWithString:url]];
Connection=[NSURLConnection connectionWithRequest:latrequest delegate:self];
[latrequest setHTTPMethod:@"POST"];
[latrequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
[latrequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[latrequest setHTTPBody:postData];
[latrequest release];

NSLog(@"latre %@",latrequest);


the code is above Please Let me know if I am missing something. Please any one Help me with that.

Answer Source

Image posting is different in iOS when we compare it with Android.

And in iOS do this formate

 NSString *urlString = [NSString stringWithFormat:@"********************/web-services/register_user.php?firstname=%@&lastname=%@&email=%@&password=%@&location=india&device=IPHONE",details.fname,details.lname,details.emailAddress,details.password];
    // urlString=[urlString stringByReplacingOccurrencesOfString:@" " withString:@"%20"];



UIImage *image=details.pic;
NSData *imageData =UIImageJPEGRepresentation(image, 0.1);
double my_time = [[NSDate date] timeIntervalSince1970];
NSString *imageName = [NSString stringWithFormat:@"%d",(int)(my_time)];
NSString *string = [NSString stringWithFormat:@"%@%@%@", @"Content-Disposition: form-data; name=\"profile_pic\"; filename=\"", imageName, @".jpg\"\r\n\""];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:string] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString*s11=   [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSDictionary *responseDictionary1;
responseDictionary1 = [XMLReader dictionaryForXMLString:s11 error:nil];

//////// This will fix issue

Regards Charan Giri

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download