2015-09-19 135 views
1

我试过这段代码来在iOS中实现多个图像。因为我想没有工作:如何在ios上传多张图片?

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 

回答

2

使用此:

-(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"); 
    }]; 
} 

本教程将帮助您:
https://charangiri.wordpress.com/2014/09/22/how-to-upload-multiple-image-to-server/

0

尝试

//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]); 

      });