Sarao
Sarao

Reputation: 379

Sending Image to server from IOS

In my IOS app I want to send an image to the server along with Image name using HTTP request.
I am a programmer with embedded Background so not aware with HTTP calls, and quite new to iPhone development also.

How can I accomplish this, any sample code or tutorials will be appreciated.

Upvotes: 2

Views: 1334

Answers (4)

Iraklii
Iraklii

Reputation: 1315

Use NSURLConnection, make sure that you convert images to NSData user_id and key there are parameters.

NSURL *URL = [NSURL URLWithString:constFileUploadURL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:60];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"0xLhTaLbOkNdArZ";

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", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"user_id\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
NSString *userid = [NSString stringWithFormat:@"%li",userID];
[body appendData:[userid dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];


[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"key\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[constBackendKey dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

for (NSData *data in arrayWithFiles)
{
        [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"files%ld\"; filename=\"image%ld.jpg\"\r\n",[arrayWithFiles indexOfObject:data],[arrayWithFiles indexOfObject:data]] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[NSData dataWithData:data]];
        [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}

[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:body];
_connection = [NSURLConnection connectionWithRequest:request delegate:self];

Upvotes: 0

Saad
Saad

Reputation: 8947

The better approach is to first compress your image using Image Compress Library Here and then upload it using and Networking library Liek AF Networking or you can also send it using NSUrlConnection. AFNetworking is easy to use. You can visit this page to see how to import this into your project then. Write these lines of codes.

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];
[manager POST:@"http://samwize.com/api/poo/"
   parameters:@{@"color": @"green"}
   constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFileURL:filePath name:@"image" error:nil];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

Upvotes: 2

Nilesh Kikani
Nilesh Kikani

Reputation: 2618

For this you can do something like this by using ASIHTTPRequest

NSURL *strUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@?action=youraction",serverUrl]];
ASIFormDataRequest *request;
request = [[[ASIFormDataRequest alloc] initWithURL:strUrl] autorelease];
[request setRequestMethod:@"POST"];
[request setTimeOutSeconds:120];

NSString *imagePAth = userImagePath;

NSArray *imageName = [userImagePath componentsSeparatedByString:@"/"];

if( userImagePath)
{
    [request setFile:imagePAth withFileName:[imageName lastObject] andContentType:@"image/jpeg"                   forKey:@"profileImage"];
}

[request setUseCookiePersistence:NO];
[request setUseSessionPersistence:NO];
[request setDelegate:self];

[request setDidFinishSelector:@selector(requestFinished:)];
[request setDidFailSelector:@selector(requestFailed:)];

[request startAsynchronous];

- (void)requestFinished:(ASIHTTPRequest *)request

{

}

- (void)requestFailed:(ASIHTTPRequest *)request
{

}

Upvotes: 0

David Ansermot
David Ansermot

Reputation: 6112

The easiest way for you from my point of view would be to use AFNetworking.

It'll send the image to a php page, then on the server, you build and save the image with the data sent.

Here's a basic tutorial, but lot of others on internet.

Upvotes: 0

Related Questions