开发者

Uploading image to a server [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 7 years ago.

开发者_JAVA百科 Improve this question

Could someone please suggest a good tutorial on how to upload an image from my iPhone application, to a server programmatically?


I would recommend the following networking library

AFNetworking


I suggest that you use the ASIHTTP Library for doing this. The sample code which comes with the library also has the appropriate examples. The library can be downloaded here : http://allseeing-i.com/ASIHTTPRequest/ .


Process 1:

  • Grab the ASIHTTPRequest api and add it to your project. see documentation here.

  • import ASIFormDataRequest.h into your class and include the delegate

Use it as

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL  URLWithString:@"upload_url"]];
request.delegate = self;
[request setFile:@"your_image_name.jpg" forKey:@"photo"];
 // or [request setFile:@"your_image_name.jpg" withFileName:@"image_name_for_server.jpg" andContentType:@"image/jpeg" forKey:@"photo"];
[request startAsynchronous];

To get acknowledgement implement delegate methods, like

- (void)uploadRequestFinished:(ASIHTTPRequest *)request
{
  NSLog(@"File upload successful"); 
}

- (void)uploadRequestFailed:(ASIHTTPRequest *)request
{ 
  NSLog(@" Error: file upload failed. Issue: \"%@\"",[[request error] localizedDescription]); 
}


N.B.: If you face the ARC problem, then simply goto the Xcode's File -> Refactor and select Convert to Objective-C ARC...

OR

Goto Project -> Targets -> Built Phases -> Compile Sources, select the Files and double click on the selected files. Write -fno-objc-arc.


Process 2:

An up-to-date way of doing so is using AFNetworking. API can be download from here


To upload image, write

//Create http client
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"/*server_url*/"]];
//convert image to data
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"your_image_name.jpg"], 0.5/*this is compressionQuality, 0.5 will reduce the image into half*/);

// for PNG image, NSData *imageData = UIImagePNGRepresentation(([UIImage imageNamed:@"your_image_name.png"]); // it doesn't support compression

//add image data to the request
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST"
                                                                     path:@"/upload"
                                                               parameters:nil
                                                constructingBodyWithBlock: ^(id <AFMultipartFormData>formData)
                                                                           {
                                                                              [formData appendPartWithFileData:imageData 
                                                                                                          name:@"image_name_for_server" 
                                                                                                      fileName:@"image_name_for_server.jpg" 
                                                                                                      mimeType:@"image/jpeg"];
                                                                           }
                              ];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite)
                                     {
                                       //this where you can monitor your upload progress
                                       NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
                                     }
];

[httpClient enqueueHTTPRequestOperation:operation];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜