Upload files to the server and upload files to the server

Source: Internet
Author: User

Upload files to the server and upload files to the server

// Upload image-(void) showActionSheet {// call out the following menu button here: myActionSheet = [[UIActionSheet alloc] initWithTitle: nil delegate: self cancelButtonTitle: @ "cancel" destructiveButtonTitle: nil otherButtonTitles: @ "enable camera", @ "get from cell phone album", nil]; [myActionSheet showInView: self. view]; [myActionSheet release];}-(void) actionSheet :( UIActionSheet *) actionSheet clickedButtonAtIndex :( NSInteger) buttonIndex {// response if (buttonInde) X = myActionSheet. cancelButtonIndex) {NSLog (@ "cancel");} switch (buttonIndex) {case 0: // open the camera to take a photo [self takePhoto]; break; case 1: // open the local album [self LocalPhoto]; break ;}// start the photo-(void) takePhoto {UIImagePickerControllerSourceType sourceType = Hangzhou; if ([UIImagePickerController isSourceTypeAvailable: Hangzhou]) {UIImagePickerController * Picker = [[UIImagePickerController alloc] init]; picker. delegate = self; // set the picker that can be edited for the image taken. allowsEditing = YES; picker. sourceType = sourceType; [picker release]; [self presentViewController: picker animated: YES completion: nil];} else {NSLog (@ "simulate that the camera cannot be turned on, please use ") ;}/// open the local album-(void) LocalPhoto {UIImagePickerController * picker = [[UIImagePickerController alloc] init]; picker. sourceType = UIImage PickerControllerSourceTypePhotoLibrary; picker. delegate = self; // set the selected image to be edited by picker. allowsEditing = YES; [self presentViewController: picker animated: YES completion: nil]; [picker release];} // enter the following link after selecting an image-(void) imagePickerController :( UIImagePickerController *) picker didFinishPickingMediaWithInfo :( NSDictionary *) info {NSString * type = [info objectForKey: UIImagePickerControllerMediaType]; // when the selected type is image If ([type ispolictostring: @ "public. image "]) {NSLog (@" you selected this image "); // convert the image to NSData UIImage * image = [info objectForKey: @" UIImagePickerControllerOriginalImage "]; NSData * data; if (UIImagePNGRepresentation (image) = nil) {data = UIImageJPEGRepresentation (image, 1.0);} else {data = UIImagePNGRepresentation (image );} // path to save the image // put the image in the documents folder of the sandbox. NSString * DocumentsPath = [NSHomeDirectory () st RingByAppendingPathComponent: @ "Documents"]; // File Manager NSFileManager * fileManager = [NSFileManager defaultManager]; // copy the data object converted from the image to the sandbox and save it as image.png [fileManager createDirectoryAtPath: documentsPath withIntermediateDirectories: YES attributes: nil error: nil]; [fileManager createFileAtPath: [DocumentsPath stringByAppendingString: @ "/image.png"] contents: data attributes: nil]; // obtain the complete path filePath of the image in the selected sandbox. = [[NSString alloc] initWithFormat: @ "% @", DocumentsPath, @ "/image.png"]; // close the album interface [picker dismissViewControllerAnimated: YES completion: nil]; [self imageUpload: image] ;}}- (void) imagePickerControllerDidCancel :( UIImagePickerController *) picker {NSLog (@ "you have canceled image Selection"); [picker dismissViewControllerAnimated: YES completion: nil];} // Method for uploading images to the server. The ASIFormDataRequest method-(void) imageUpload :( UIImage *) image {U IImage * im = [UIImage imageWithContentsOfFile: filePath]; // obtain the image NSData * data = UIImagePNGRepresentation (im) through the path of the image ); // obtain image data/* There are two methods for obtaining images in ios: UIImageJPEGRepresentation, one is UIImagePNGRepresentation. The former obtains much smaller data than the latter .. * // Server address NSURL * url = [NSURL URLWithString: @ "Write Your Server Upload address here"]; aRequest = [[ASIFormDataRequest alloc] initWithURL: url]; [aRequest setDelegate: self]; // proxy [aRequest setRequestMethod: @ "POST"]; // this parameter is not very clear [aRequest addData: data withFileName: @ "test.png" andContentType: @ "image/png" forKey: @ "file"]; [aRequest addRequestHeader: @ "Content-Type" value: @ "multipart/form-data"]; // The value here must be consistent with that on the server [aReques T startAsynchronous]; // start. Asynchronous [aRequest setDidFinishSelector: @ selector (headPortraitSuccess)]; // The headPortraitSuccess method is automatically triggered when the request succeeds [aRequest setDidFailSelector: @ selector (headPortraitFail)]; // if the request fails, the headPortraitFail method is automatically triggered.}-(void) headPortraitSuccess {NSData * myResponseData = [aRequest responseData]; // when obtaining data: during debugging, the value of myResponseData is always nil. Why? // Convert the received data to the NSString type and print the NSString * result = [[NSString alloc] initWithData: myResponseData encoding: NSUTF8StringEncoding]; NSLog (@ "result ------------> % @", result); NSDictionary * data = [result encoding: JKParseOptionLooseUnicode]; NSArray * imgArray = [data objectForKey: @ "data"]; NSString * imgpath = @ "http://twww.51qed.com"; if ([imgArray count]) {NSDictionary * imgInfo = [ImgArray objectAtIndex: 0]; imgpath = [imgpath stringByAppendingString: [imgInfo objectForKey: @ "img"]; NSLog (@ "imgpath ------------ >%@", imgpath );} // set H5 image updates. ios calls the JS Code [webview stringByEvaluatingJavaScriptFromString: [NSString stringWithFormat: @ "setNewImg ('% @');", imgpath]; NSLog (@ "Upload successful! "); [ARequest release];}-(void) headPortraitFail {NSLog (@" Upload Failed! ");} // Start request-(void) requestStarted :( ASIHTTPRequest *) request {NSLog (@" Start request! ");} @ End
Import required
<pre name="code" class="objc">#import "ASIHTTPRequest.h"#import "ASIFormDataRequest.h"#import "JSONKit.h"

These three header files
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.