-1

In my project:-

1)click a picture from camera(don't save it).

2)show a cropped image with 2 buttons post and cancel.where post button post the image and cancel button discard the image.it looks something like this. 2nd

I don't know how to post the camera capture image.I searched over the internet find a lot of codes but didn't find any code that helps me out.

I tried code given on these links..

First ,Second ,Third, and many others.

I wish someone will help..how to post?? thanks.

-(IBAction)sendRequest:(id)sender {

     #define DataDownloaderRunMode @"myapp.run_mode" 

        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url 
                                       cachePolicy:NSURLRequestUseProtocolCachePolicy 
                                       timeoutInterval:60]; 

        [request setHTTPMethod:@"POST"]; 

        NSString *boundary = @"----WebKitFormBoundarycC4YiaUFwM44F6rT";

        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:@"Content-Disposition: form-data; name="attachment[file]";
        filename="picture.png"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

        [body appendData:[[NSString stringWithString:@"Content-Type: image/png\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];

        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request 
                                                               delegate:self 
                                                               startImmediately:NO]; 

        [connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:DataDownloaderRunMode]; 

        [connection start];
}

and connecting this to post button..

Community
  • 1
  • 1
Kundan
  • 3,325
  • 2
  • 26
  • 65
  • 2
    that first link does exactly what you want, if you are having specific problems you should post your code and any errors – wattson12 Dec 27 '12 at 10:04
  • @wattson12 when i use code of first link it shows error in this line...[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name="attachment[file]"; filename="picture.png"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; – Kundan Dec 27 '12 at 10:05
  • @wattson12 the error is expected ' '.but i don't know where.... – Kundan Dec 27 '12 at 10:07
  • @user1865424 you use the link which i give you dude?? follow that tutorials?? – Paras Joshi Dec 27 '12 at 10:11
  • show your code? what's the response you are getting from server? – NSCry Dec 27 '12 at 10:15
  • @NSIllusion my code shows error..so,no response getting...but i am showing my code..i am editing above... – Kundan Dec 27 '12 at 10:16
  • @user1865424: Have you setup the delegate methods for `NSURLConnection` ? Use them to fetch response from the webserver to which you are uploading the image. And can you edit your question with those error messages ? – sunhsiv Dec 27 '12 at 11:58
  • @Rajneesh071 no not yet...now i posting is happening but image not get posted....can u help?? – Kundan Dec 28 '12 at 07:44

2 Answers2

3

UPDATE:

[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"picture\"; filename=\"picture.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

Here your database field name for store the image is picture then write the field name in name parameter...

use this code...

Paras Joshi
  • 19,954
  • 11
  • 54
  • 69
2
// add image data
NSData *imageData = UIImageJPEGRepresentation(imageToPost, 1.0);
if (imageData) {
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:imageData];
    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}  

Follow this answer Link

Or you can use ASIHTTP classes for this

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://URL/Service.asmx/RegisterClient?"]];
            request.delegate=self;

            encodedImage = UIImagePNGRepresentation(imgRef);

            [[NSUserDefaults standardUserDefaults]setObject:UIImagePNGRepresentation(imgRef) forKey:@"userImage"];

            str =[Base64 encode:encodedImage];

           [request setPostValue:email.text forKey:@"Email"];

            [request setPostValue:name.text forKey:@"FName"];
            [request setPostValue:str forKey:@"UserImage"];
            [request setPostValue:dob.text forKey:@"dob"];
            [request setPostValue:phoneNumber.text forKey:@"Phone"];
            [request setPostValue:ggg.text forKey:@"Gender"];
            [request setPostValue:address.text forKey:@"Address"];
            [request setPostValue:interest.text forKey:@"Interest"];

            [request startAsynchronous];

ASIHTTPRequest documentation

Community
  • 1
  • 1
Rajneesh071
  • 29,619
  • 13
  • 57
  • 72