如何在ios中上载多个图像?
我尝试过用这段代码在iOS中实现多个图像。没有按我想要的那样工作:如何在ios中上载多个图像?,ios,objective-c,nsurlrequest,Ios,Objective C,Nsurlrequest,我尝试过用这段代码在iOS中实现多个图像。没有按我想要的那样工作: NSString *BoundaryConstant = @"----------V2ymHFg03ehbqgZCaKO6jy"; // string constant for the post parameter 'file'. My server uses this name: `file`. Your's may differ NSString* FileParamConstant = @"fileToUpload[]"
NSString *BoundaryConstant = @"----------V2ymHFg03ehbqgZCaKO6jy";
// string constant for the post parameter 'file'. My server uses this name: `file`. Your's may differ
NSString* FileParamConstant = @"fileToUpload[]";
NSArray *arrayfile=[NSArray arrayWithObjects:FileParamConstant,FileParamConstant ,nil];
// the server url to which the image (or the media) is uploaded. Use your server url here
NSURL* requestURL = [NSURL URLWithString:@"http://jackson.amtpl.in/tranzop/api/customertest/postorder"];
// create request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:30];
[request setHTTPMethod:@"POST"];
// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", BoundaryConstant];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];
// post body
NSMutableData *body = [NSMutableData data];
// add params (all params are strings)
for (NSString *param in _params) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", [_params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}
for (int i=0; i<upload_img_array.count; i++) {
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:[upload_img_array objectAtIndex:0]],1); // add image data
// NSData *imageData = UIImageJPEGRepresentation([upload_img_array objectAtIndex:i], 1.0);
if (imageData) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image%d.png\"\r\n", FileParamConstant,i] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/png\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
}
}
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// set
NSString*BoundaryConstant=@“---V2ymHFg03ehbqgZCaKO6jy”;
//post参数“file”的字符串常量。我的服务器使用此名称:`file`。你的想法可能不同
NSString*FileParamConstant=@“fileToUpload[]”;
NSArray*arrayfile=[NSArray arrayWithObjects:FileParamConstant,FileParamConstant,nil];
//将图像(或媒体)上载到的服务器url。在此处使用您的服务器url
NSURL*requestURL=[NSURL URLWithString:@]http://jackson.amtpl.in/tranzop/api/customertest/postorder"];
//创建请求
NSMutableURLRequest*请求=[[NSMutableURLRequest alloc]init];
[请求设置缓存策略:NSURLRequestReloadIgnoringLocalCacheData];
[请求设置HttpShouldHandleCookies:否];
[请求setTimeoutInterval:30];
[请求设置HttpMethod:@“POST”];
//在HTTP头中设置内容类型
NSString*contentType=[NSString stringWithFormat:@“多部分/表单数据;边界=%@”,边界常数];
[请求设置值:HttpHeaderField的contentType:@“内容类型”];
//柱体
NSMutableData*正文=[NSMutableData];
//添加参数(所有参数都是字符串)
for(NSString*参数在_参数中){
[body appendData:[[NSString stringWithFormat:@”--%@\r\n,“边界常数]数据使用编码:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@“内容处理:表单数据;名称=\”%@\“\r\n\r\n”,参数]数据使用编码:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@“%@\r\n”,[\u params objectForKey:param]]dataUsingEncoding:NSUTF8StringEncoding]];
}
对于(int i=0;i使用以下公式:
-(void)uploadImages{
// image.finalImage - is image itself
// totalCount - Total number of images need to upload on server
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSDictionary *parameters = @{@"Key":@"Value",@"Key":@"Value"};
[manager POST:serverURL parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:UIImagePNGRepresentation(image.finalImage) name:@"ImageName" fileName:[[Helper getRandomString:8] stringByAppendingString:@".png"] mimeType:@"image/png"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:(NSData *)responseObject options:kNilOptions error:nil];
_uploadCounter+=1;
if(_uploadCounter<totalCount){
[self uploadImages];
}else {
NSLog(@"Uploading all images done");
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error");
}];
}
-(无效)上载图像{
//image.finalImage-是图像本身
//totalCount-需要在服务器上上载的图像总数
AFHTTPRequestOperationManager*manager=[AFHTTPRequestOperationManager];
manager.responseSerializer=[AFHTTPResponseSerializer序列化程序];
NSDictionary*参数=@{@“键”:@“值”,@“键”:@“值”};
[manager POST:serverURL参数:参数constructingBodyWithBlock:^(id试试看
//img_send is a UIImage with u want to send
NSData *imageData = UIImagePNGRepresentation(img_send);
str_img= [imageData base64EncodedStringWithOptions:0];
NSDictionary *post_dir = [[NSMutableDictionary alloc] init];
[post_dir setValue:str_img forKey:@"image_string"];
NSData *jsData = [NSJSONSerialization dataWithJSONObject:post_dir options:NSJSONWritingPrettyPrinted error:nil];
NSMutableData *data = [[NSMutableData alloc] init];
self.receivedData = data;
NSURL *url = [NSURL URLWithString:@"YOUR URL STRING"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[url standardizedURL]];
//set http method
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [jsData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:jsData];
//initialize a connection from request
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
self.connection=connection;
//start the connection
[connection start];
NSHTTPURLResponse *response = nil;
NSError *error = nil;
NSData *respData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"~~~~~ Status code: %d", [response statusCode]);
});