Advertisement
Guest User

Untitled

a guest
Jan 15th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. - (void)uploadImagedFromArray:(NSArray *)images
  2. {
  3. UIImage *image = [images objectAtIndex:0];
  4. NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
  5. NSURL *serviceURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@/api/mobile/image", [infoDictionary objectForKey:@"ServiceURL"]]];
  6.  
  7. NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:serviceURL];
  8. NSData *imgData = UIImageJPEGRepresentation(image, 1.0);
  9.  
  10. [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
  11. [request setHTTPShouldHandleCookies:NO];
  12. [request setTimeoutInterval:60];
  13. [request setHTTPMethod:@"POST"];
  14.  
  15. NSString *boundary = @"consocio-boundary";
  16.  
  17. // set Content-Type in HTTP header
  18. NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
  19. [request setValue:contentType/*@"application/octet-stream"*/ forHTTPHeaderField:@"Content-Type"];
  20.  
  21. //post body
  22. NSMutableData *body = [NSMutableData data];
  23.  
  24. // add params
  25. [body appendData:[[NSString stringWithFormat:@"--%@rn", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  26. [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=%@rnrn", @"Image Caption"] dataUsingEncoding:NSUTF8StringEncoding]];
  27. [body appendData:[[NSString stringWithFormat:@"%@rn", @"Some Caption"] dataUsingEncoding:NSUTF8StringEncoding]];
  28.  
  29. if (imgData)
  30. {
  31. [body appendData:[[NSString stringWithFormat:@"--%@rn", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  32. [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=%@rnrn", @"Image Caption"] dataUsingEncoding:NSUTF8StringEncoding]];
  33. [body appendData:[@"Content-Type: image/jpegrnrn" dataUsingEncoding:NSUTF8StringEncoding]];
  34. [body appendData:imgData];
  35. [body appendData:[[NSString stringWithFormat:@"rn"] dataUsingEncoding:NSUTF8StringEncoding]];
  36. }
  37.  
  38. [request setHTTPBody:body];
  39.  
  40. NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[body length]];
  41. [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
  42.  
  43. NSString *authentication = nil;
  44. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  45. NSLog(@"User: %@, Password: %@", [defaults objectForKey:@"RTMUser"], [defaults objectForKey:@"RTMPassword"]);
  46. authentication = [NSString stringWithFormat:@"%@:%@", [defaults objectForKey:@"RTMUser"]/*@"kyleh@redtouchmedia.com"*/, [defaults objectForKey:@"RTMPassword"]/*@"123456"*/];
  47.  
  48. NSData *authenticationData = [authentication dataUsingEncoding:NSASCIIStringEncoding];
  49. NSString *authenticationValue = [NSString stringWithFormat:@"Basic %@", [ConsocioAPI base64EncodeData:authenticationData]];
  50. [request setValue:authenticationValue forHTTPHeaderField:@"Authorization"];
  51.  
  52. NSURLSession *session = [NSURLSession sharedSession];
  53. [[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
  54. if (data.length > 0)
  55. {
  56. NSLog(@"Success");
  57. }
  58. }] resume];
  59.  
  60. //[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
  61.  
  62. //}];
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement