How to upload images with text using iPhone SDK using ASIHttpRequest ?
2014-02-20 14:55
656 查看
How to upload images with text using iPhone SDK using ASIHttpRequest ?
Posted on July4, 2011 by Nimit
Parekh
This example shows how to upload images using iPhone SDK. In this example we pick multiple images from iPhone Image Gallery or from iPhone Camera and send them to server using ASIHttpRequest.
Step 1 :- Download Latest version of the AsiHttp framework.
Step 2 :- Copy the Folder of ASI and paste in to your project.
![](http://i2.wp.com/sugartin.info/wp-content/uploads/2011/07/Picture-23.png?resize=209%2C411)
Step 3 :- Following Framework import into your projects.
SystemConfiguration.framework
MobileCoreServices.framework
CoreGraphics.framework
libz.1.2.3.dylib
For more Details
click here & Then start placing code as follows into your project.
Code for .h file.
#import @interface RootViewController : UIViewController { NSMutableArray *arrImages; IBOutlet UIButton *btnImage1; IBOutlet UIButton *btnImage2; IBOutlet UIButton *btnImage3; IBOutlet UIButton *btnImage4; } - (IBAction)btnTakePicture_Clicked:(id)sender; - (IBAction)btnPostImages_Clicked:(id)sender; @end
Code for .m file.
#import "RootViewController.h" #import "ASIFormDataRequest.h" @implementation RootViewController int intButton; - (void)viewDidLoad { [super viewDidLoad]; self.title = @"Select Images"; arrImages = [[NSMutableArray alloc]init]; }
- (IBAction)btnTakePicture_Clicked:(id)sender { UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select Image from..." delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Camera", @"Image Gallary", nil]; actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent; actionSheet.alpha=0.90; actionSheet.tag = 1; [actionSheet showInView:self.view]; [actionSheet release]; UIButton *btn = (UIButton *)sender; intButton = btn.tag; }
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { switch (actionSheet.tag) { case 1: switch (buttonIndex) { case 0: { #if TARGET_IPHONE_SIMULATOR UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"Camera not available." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; [alert release]; #elif TARGET_OS_IPHONE UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.sourceType = UIImagePickerControllerSourceTypeCamera; picker.delegate = self; //picker.allowsEditing = YES; [self presentModalViewController:picker animated:YES]; [picker release]; #endif } break; case 1: { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; picker.delegate = self; [self presentModalViewController:picker animated:YES]; [picker release]; } break; } break; default: break; } }
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info { NSData *dataImage = UIImageJPEGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"],1); UIImage *img = [[UIImage alloc] initWithData:dataImage]; if (intButton == 1) { [btnImage1 setBackgroundImage:img forState:UIControlStateNormal]; [btnImage1 setImage:nil forState:UIControlStateNormal]; } else if (intButton == 2) { [btnImage2 setBackgroundImage:img forState:UIControlStateNormal]; [btnImage2 setImage:nil forState:UIControlStateNormal]; } else if (intButton == 3) { [btnImage3 setBackgroundImage:img forState:UIControlStateNormal]; [btnImage3 setImage:nil forState:UIControlStateNormal]; } else { [btnImage4 setBackgroundImage:img forState:UIControlStateNormal]; [btnImage4 setImage:nil forState:UIControlStateNormal]; } [arrImages addObject:dataImage]; [picker dismissModalViewControllerAnimated:YES]; }
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { [self.navigationController dismissModalViewControllerAnimated:YES]; }
- (IBAction)btnPostImages_Clicked:(id)sender { if ([arrImages count] > 0) { NSString *strURL = @"Write Your URL Here."; ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:strURL]]; [request setDelegate:self]; [request setPostValue:@"This is sample text..." forKey:@"text"]; for (int i = 0; i < [arrImages count]; i++) { [request addData:[arrImages objectAtIndex:i] withFileName:@"image.jpg" andContentType:@"image/jpeg" forKey:[NSString stringWithFormat:@"image%d", i + 1]]; } [request startAsynchronous]; } else { UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Message" message:@"Please select images..." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alertView show]; [alertView release]; } }
- (void)requestFinished:(ASIHTTPRequest *)request { NSString *receivedString = [request responseString]; UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Message" message:receivedString delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alertView show]; [alertView release]; }
- (void)requestFailed:(ASIHTTPRequest *)request { NSString *receivedString = [request responseString]; UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Message" message:receivedString delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alertView show]; [alertView release]; }
Give Me the Code.
Here
is the source code for it.
ASIFormDataRequest *uploadImageRequest= [ ASIFormDataRequest requestWithURL :
[NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
]];
[uploadImageRequest setStringEncoding:NSUTF8StringEncoding];
[uploadImageRequest setRequestMethod:@"POST"];
[uploadImageRequest setPostValue:studentsID forKey:@"studentId"];
[uploadImageRequest setPostValue:classId forKey:@"classId"];
[uploadImageRequest setPostValue:userId forKey:@"userId"];
[uploadImageRequest setPostValue:kindId forKey:@"kindId"];
[uploadImageRequest setPostValue:partitionCode forKey:@"partitionCode"];
[uploadImageRequest setPostFormat:ASIMultipartFormDataPostFormat];
for (UIImage *eImage in imagesArray)
{
NSData *imageData=UIImageJPEGRepresentation(eImage,100);
NSString *photoName=[NSString stringWithFormat:@"%@.jpg",[self dateString]];
NSString *photoDescribe=@" ";
NSLog(@"photoName=%@",photoName);
NSLog(@"photoDescribe=%@",photoDescribe);
NSLog(@"图片大小+++++%d",[imageData length]/1024);
//照片content
[uploadImageRequest setPostValue:photoDescribe forKey:@"photoContent"];
[uploadImageRequest addData:imageData withFileName:photoName andContentType:@"image/jpeg" forKey:@"photoContent"];
}
[uploadImageRequest setDelegate : self ];
[uploadImageRequest setDidFinishSelector : @selector (responseComplete:)];
[uploadImageRequest setDidFailSelector : @selector (responseFailed:)];
[uploadImageRequest startAsynchronous];
相关文章推荐
- How to upload a file via a HTTP multipart request in Java without using any external libraries
- How to deal with "Incorrect NSStringEncoding value 0×0000 Error with ASIHTTPRequest" problem?
- Using RestTemplate, how to send the request to a proxy first so I can use my junits with JMeter?
- How to get blob data using javascript XmlHttpRequest by sync
- How to Send an HTTP Header With Every Request With Spring RestTemplate
- How to Make an HTTP Connection Using TCP/IP with RSocket
- How to configure XDB for using ftp and http protocols with ASM [ID 357714.1]
- How to disable 'withcredentials' in HTTP header with node.js and Request package?
- How to get the password text in a text with password property from another process using C++ - 用C++如何从不同进程获取密码框文本
- iPhone - How to send image + text on facebook using sharekit
- How to use ASIHTTPRequest
- How To Do A JavaScript Cross-Domain POST or GET With jQuery or XMLHttpRequest
- How to Make an HTTP Connection Using TCP/IP with RSocket
- scala: How to write a simple HTTP GET request client in Scala (with a timeout)
- How to Make an HTTP Connection Using TCP/IP with RSocket
- How to configure XDB for using ftp and http protocols with ASM [ID 357714.1]
- How to Make an HTTP Connection Using TCP/IP with RSocket
- 添加asihttprequest框架到iPhone/iPad工程中使用
- Google Volley: How to send a POST request with Json data?
- How to access HTTP server using Telnet - 如何使用 Telnet 访问 Http 服务器