-3

I try to pass the image on json and store on it but it couldn't pass on that server. i have make code for that so how to pass that image on json and how to convert that image into string and store on json.

NSString *urlSTR = [NSString stringWithFormat:@"http://IOSAPI/registration.php?Profile_Picture&User_Name=%@&First_Name=%@&Last_Name=%@&Email_ID=%@&Password=%@",_textFieldUserName.text,_textFieldFirstName.text,_textFieldLastName.text,_textFieldEmail.text,_textFieldPassward.text];

    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:urlSTR] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:30.0];

    NSURLResponse *responce;

    NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&responce error:nil];

    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

    NSLog(@"RESPONCE %@",dictionary);

    NSLog(@"RESPONCE %@",[dictionary valueForKey:@"status"]);

    NSString *str =  [NSString stringWithFormat:@"%@", [dictionary valueForKey:@"status" ]];

    UIImage *images=self.imageView2.image;
    NSData *imageData =UIImageJPEGRepresentation(images, 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=\"picture\"; filename=\"", imageName, @".jpg\"\r\n\""];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:urlSTR]];
    [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];
    NSLog(@"%@",s11);
  • Convert your image into Base64 string NSData *imageData = UIImagePNGRepresentation(image); NSString *imageString = [imageData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed]; – Pradip Vanparia May 20 '15 at 09:13
  • @PradipVanparia how to pass that converted image into json query – Jeni Willget May 20 '15 at 09:16
  • possible duplicate of [ios Upload Image and Text using HTTP POST](http://stackoverflow.com/questions/8564833/ios-upload-image-and-text-using-http-post) – Nirav Gadhiya May 20 '15 at 09:21

2 Answers2

2
NSString *string = [NSString stringWithFormat:@"%@%@%@", @"Content-Disposition: form-data; name=\"profile_pic\"; filename=\"", imageName, @".jpg\"\r\n\""];
Pradip Vanparia
  • 1,742
  • 10
  • 22
0
NSData *data = UIImageJPEGRepresentation(images, 1.0);
NSString *StrCoverImageData = [data base64EncodedStringWithOptions:0];

use this string to send over server.

VD Patel
  • 286
  • 1
  • 6
  • But how to pass that converted string in the API.Bcz it shows encrypted data. – Jeni Willget May 20 '15 at 09:22
  • you can pass it like other string with appropriate key. i am using AFNetworking and working fine with this. [params setValue:StrCoverImageData forKey:@"profile_image"]; – VD Patel May 20 '15 at 09:29