Created
February 26, 2012 11:13
-
-
Save zbuc/1916108 to your computer and use it in GitHub Desktop.
NSImage base64 HTTP POST
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
- (void)uploadImage:(NSImage *)image | |
{ | |
NSData *imageData = [image TIFFRepresentation]; | |
NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imageData]; | |
imageData = [imageRep representationUsingType:NSPNGFileType properties:nil]; | |
NSString *base64 = [imageData encodeBase64WithNewlines: NO]; | |
NSString *jsonRequest = @"key=92428d1a5839df89cb8e87e8a31cd935&image="; | |
jsonRequest = [jsonRequest stringByAppendingString:[base64 stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]]; | |
NSLog(@"Request: %@", jsonRequest); | |
NSData *requestData = [NSData dataWithBytes: [jsonRequest UTF8String] length: [jsonRequest length]]; | |
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://api.imgur.com/2/upload"]]; | |
[request setHTTPMethod: @"POST"]; | |
[request setHTTPBody: requestData]; | |
NSData *returnData = [ NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil ]; | |
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; | |
NSLog(@"Returned Json: %@", returnString); | |
} |
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
- (id)initWithFrame:(NSRect)frame | |
{ | |
self = [super initWithFrame:frame]; | |
if (self) { | |
NSArray *dragTypes = [NSArray arrayWithObjects:NSURLPboardType, NSFileContentsPboardType, NSFilenamesPboardType, nil]; | |
[self registerForDraggedTypes:dragTypes]; | |
} | |
return self; | |
} | |
//perform the drag and log the files that are dropped | |
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender | |
{ | |
NSPasteboard *pb = [sender draggingPasteboard]; | |
if([[pb pasteboardItems] count] != 1){ | |
return NO; | |
} | |
if([NSBitmapImageRep canInitWithPasteboard:pb]){ | |
NSImage *image = [[NSImage alloc] initWithPasteboard:pb]; | |
[[[ScreenshotController alloc] autorelease] uploadImage:image]; | |
return YES; | |
} | |
return NO; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment