Created
February 23, 2012 21:10
-
-
Save shiftregister-vg/1895074 to your computer and use it in GitHub Desktop.
uploadImage method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+ (BOOL)uploadImage:(UIImage *)image withName:(NSString *)fileName toURL:(NSURL *)url { | |
NSData *imageData = UIImageJPEGRepresentation(image, 100); | |
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; | |
[request setHTTPMethod:@"POST"]; | |
NSString *boundary = @"0x0hHai1CanHazB0undar135"; | |
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary]; | |
[request setValue:contentType forHTTPHeaderField:@"Content-Type"]; | |
NSMutableData *body = [NSMutableData data]; | |
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding: NSUTF8StringEncoding]]; | |
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"imageToAttach\"; filename=\"%@\"\r\n",fileName]dataUsingEncoding:NSUTF8StringEncoding]]; | |
[body appendData:[[NSString stringWithFormat:@"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; | |
[body appendData:imageData]; | |
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; | |
[request setHTTPBody:body]; | |
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; | |
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; | |
NSLog(@"%@",returnString); | |
return YES; | |
} |
Bet you never thought you'd have to craft your own http requests eh? Definitely something I've always taken for granted.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That is one of the scariest snippets of code I've ever seen... and I've written a Hello World app in Brainf*ck. There's so few spaces.