Skip to content

Instantly share code, notes, and snippets.

@ylem
Created January 26, 2017 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ylem/dc8332047d030709859135417bf1531c to your computer and use it in GitHub Desktop.
Save ylem/dc8332047d030709859135417bf1531c to your computer and use it in GitHub Desktop.
Upload image by NSURLSession
/**
* Generate a mutable URLRequest with authorisation Token (if has) and User-Agent
*
* @param url NSURL object
* @param method GET, POST, PUT or DELETE
* @return NSMutableURLRequest object, can be modify (add more values) later.
*/
- (NSMutableURLRequest *)requestUrl:(NSURL *)url method:(NSString *)method {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:kRequestTimeOut];
request.HTTPMethod = method;
/// set request header
NSString *appMajorVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
NSString *appMinorVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
NSString *versionString = [NSString stringWithFormat:@"iOS.%@.%@",appMajorVersion, appMinorVersion];
[request setValue:versionString forHTTPHeaderField:@"User-Agent"];
// default 'Content-Type'
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
return request;
}
- (void)uploadImage:(NSString *)urlString image:(UIImage *)image completion:(void(^)(BOOL succeed))completion {
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *req = [self requestUrl:url method:@"POST"];
NSString *boundary = [[NSUUID UUID] UUIDString];
// set header
[req setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField:@"Content-Type"];
// filename is xing.jpg - image is convert by UIImageJPEGRepresentation().
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"file\"; filename=\"xing.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:UIImageJPEGRepresentation(image, 0.8)]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionUploadTask *task = [_session uploadTaskWithRequest:req fromData:body completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (completion) {
dispatch_async(dispatch_get_main_queue(), ^{
BOOL succeed = [self succeedRequest:(NSHTTPURLResponse *)response error:error];
completion(succeed);
});
}
}];
[task resume];
[_tasks addObject:task];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment