Skip to content

Instantly share code, notes, and snippets.

@narup
Created November 12, 2014 06:24
Show Gist options
  • Save narup/20f089b4c75c7d5c2570 to your computer and use it in GitHub Desktop.
Save narup/20f089b4c75c7d5c2570 to your computer and use it in GitHub Desktop.
- (void)uploadImage:(UIImage *)image
completionHandler:(void (^)(NSDictionary *uploadResponse, NSError *error))completionHandler {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
[self get:[self uploadURL] completionHandler:^(id responseData, NSError *error) {
if (error) {
completionHandler(nil, error);
} else if (!responseData) {
completionHandler(nil, [NSError errorWithDomain:@"Upload Failed" code:500 userInfo:nil]);
} else {
NSDictionary *response = (NSDictionary *)responseData;
NSLog(@"response:%@",response);
NSURL *url = [NSURL URLWithString:[response objectForKey:@"data"]];
NSString *imageFileName = [[NSUUID UUID] UUIDString];
[self uploadImageToGoogleCloud:image uploadUrl:url fileName:imageFileName completionHandler:completionHandler];
}
}];
}
- (void)uploadImageToGoogleCloud:(UIImage *)image
uploadUrl:(NSURL *)uploadURL
fileName:(NSString *)fileName
completionHandler:(void (^)(NSDictionary *uploadResponse, NSError *error))completionHandler {
NSData *imageData = UIImagePNGRepresentation(image);
NSString *boundary = @"0xKhTmLbOuNdArY";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:uploadURL];
[request setHTTPMethod:@"POST"];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *upLoadSession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
NSMutableData *body = [NSMutableData data];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n",fileName]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/png\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionUploadTask *uploadTask = [upLoadSession uploadTaskWithRequest:request
fromData:body
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*) response;
int statusCode = httpResponse.statusCode;
if (statusCode == 200) {
NSDictionary *responseJSON = [self responseJSON:data];
NSLog(@"RESPONSE:%@", responseJSON);
completionHandler (responseJSON, nil);
} else {
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
completionHandler (nil, [NSError errorWithDomain:responseString code:500 userInfo:nil]);
}
}];
[uploadTask resume];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment