I'm trying to upload a single UIImage to a server, and everything seems to be okay except that the image is never uploaded.
This is the code I'm using to upload the image in iOS:
const NSString *boundaryConstant = #"----------V2ymHFg03ehbqgZCaKO6jy";
const NSString *fileParamConstant = #"photo";
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:nil];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:#"POST"];
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundaryConstant];
[request setValue:contentType forHTTPHeaderField:#"Content-Type"];
NSMutableData *body = [NSMutableData data];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library assetForURL:info[UIImagePickerControllerReferenceURL] resultBlock:^(ALAsset *asset) {
ALAssetRepresentation *representation = [asset defaultRepresentation];
// get byte size of image
long long size = [representation size];
unsigned char *bytes = malloc(size);
// read image data into bytes array
[representation getBytes:bytes fromOffset:0 length:size error:nil];
NSData *imageData = [NSData dataWithBytesNoCopy:bytes length:size freeWhenDone:YES];
if (imageData) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"; filename=\"%#\"\r\n", fileParamConstant, filename] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Type: %#\r\n\r\n",[SWNetworkController contentTypeForImageData:imageData]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:#"--%#--\r\n", boundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
NSString *postLength = [NSString stringWithFormat:#"%zu", [body length]];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody:body];
NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromData:imageData completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(#"STRING %#", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
NSLog(#"%#", response);
NSLog(#"%#", error);
}];
[uploadTask resume];
} failureBlock:^(NSError *error) {
NSLog(#"Image error:\n%#",error);
}];
The server responds with a 200 OK status and no error, except the image is not received and nothing is returned from the server (this is expected if no image is uploaded).
Here is the server-side code:
<?
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["photo"]["name"]);
$extension = end($temp);
error_log(print_r($_FILES, true));
if ((($_FILES["photo"]["type"] == "image/gif")
|| ($_FILES["photo"]["type"] == "image/jpeg")
|| ($_FILES["photo"]["type"] == "image/jpg")
|| ($_FILES["photo"]["type"] == "image/pjpeg")
|| ($_FILES["photo"]["type"] == "image/x-png")
|| ($_FILES["photo"]["type"] == "image/png"))
&& ($_FILES["photo"]["size"] < 20000000)
&& in_array($extension, $allowedExts)) {
if ($_FILES["photo"]["error"] == 0) {
$filename = sha1($_FILES['photo']['name'] . uniqid("",true));
if (move_uploaded_file($_FILES['photo']['tmp_name'], 'pic/' . $filename . "." . $extension)) {
// do stuff with the saved image here
}
}
}
?>
A normal request (via the web interface) logs the following:
Array
(
[photo] => Array
(
[name] => BoPzSyRIgAAe1h6.jpg-large.jpeg
[type] => image/jpeg
[tmp_name] => /var/tmp/phpjScXQB
[error] => 0
[size] => 67900
)
)
Meanwhile, the request sent from iOS looks as follows:
Array
(
)
For the life of me, I can't figure out what's going wrong... Any ideas?
Thanks
The problem was in NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromData:imageData completionHandler:...]
NSURLSessionUploadTask ignores the request body provided and adds the fromData: parameter as the request body. However, I was just providing the image data and not the properly formatted request body...
The fixed code is then NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromData:body completionHandler:...]
I'll leave my answer up in case anyone else stumbles across this problem again.
Related
I use the following code to upload images to the server:
void call_MediaUploadAPI(NSArray *parameters, NSString *url, BOOL isAnUpdate, SuccessBlock successBlock, FailureBlock failureBlock)
{
NSString *boundary = #"----WebKitFormBoundary7MA4YWxkTrZu0gW";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#%#", BASEURL, url]]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:600.0];
[request setHTTPMethod:#"POST"];
NSString *token = [[NSUserDefaults standardUserDefaults]valueForKey:TOKEN];
NSString *authValue = [NSString stringWithFormat: #"JWT %#",token];
[request setValue:authValue forHTTPHeaderField:#"Authorization"];
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[request setValue:contentType forHTTPHeaderField: #"Content-Type"];
NSError *error;
NSMutableData *body = [NSMutableData data];
for (NSDictionary *param in parameters) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
if (param[#"fileName"]) {
NSString *mimetype = [[NetworkIntractor sharedManager] mimeTypeForPath:param[#"fileName"]];
NSData *imageData = [NSData dataWithContentsOfFile:param[#"fileName"]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition:form-data; name=\"%#\"; filename=\"%#\"\r\n", param[#"name"], param[#"fileName"]]dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Type: %#\r\n\r\n", mimetype]dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
if (error) {
NSLog(#"%#", error);
}
} else {
[body appendData:[[NSString stringWithFormat:#"Content-Disposition:form-data; name=\"%#\"\r\n\r\n", param[#"name"]]dataUsingEncoding:NSUTF8StringEncoding]];
if ([param[#"name"] isEqualToString:#"data"]) {
[body appendData:[NSJSONSerialization dataWithJSONObject:param[#"value"] options:0 error:nil] ];
}else{
[body appendData:[[NSString stringWithFormat:#"%#", param[#"value"]]dataUsingEncoding:NSUTF8StringEncoding] ];
}
}
}
[body appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n", boundary]dataUsingEncoding:NSUTF8StringEncoding] ];
[request setHTTPBody:body];
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (error == nil) {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
if (dict != nil) {
NSLog(#"multipart api response : %#", dict);
successBlock(dict,response);
}else
{
NSLog(#"multipart api failure response : %#", error);
failureBlock(error,response);
}
}}];
[task resume];
}
I tried giving different combination of timeout intervals like this:
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
config.timeoutIntervalForRequest = 150.0f;
config.timeoutIntervalForResource = 300.0f;
self.session = [NSURLSession sessionWithConfiguration:config delegate:nil delegateQueue:nil];
But, for a bit larger image files, I never get response back from the server though the image gets uploaded as I can find them available in the admin panel.
I am making a post request using multipart form-data. with the following code I can able to send the image but when I go the backend server and check, the image that I sent is rotating there . anyone have idea why it is happening ?
NSURL *reqUrl = [NSURL URLWithString:self.dataModel.apiUrl];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:reqUrl];
NSString *boundary = #"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request addValue:contentType forHTTPHeaderField: #"Content-Type"];
[request setHTTPMethod:#"POST"];
NSDictionary *requestBody = self.dataModel.requestParams2;
UIImage *imageToUpload = [requestBody objectForKey:keyUploadImage];
if(requestBody){
NSMutableData *body = [NSMutableData data];
float low_bound = 0;
float high_bound =5000;
float rndValue = (((float)arc4random()/0x100000000)*(high_bound-low_bound)+low_bound);//image1
int intRndValue = (int)(rndValue + 0.5);
NSString *str_image1 = [#(intRndValue) stringValue];
NSData *imageData = UIImageJPEGRepresentation(imageToUpload, 0);
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"uploadedfile1\"; filename=\"%#.jpeg\"\n",str_image1] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"--%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
}
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:30];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
NSLog(#"completionHandler with response:%#",[NSHTTPURLResponse localizedStringForStatusCode:[(NSHTTPURLResponse*)response statusCode]]);
NSLog(#"reponse: %ld",(long)[(NSHTTPURLResponse*)response statusCode]);
NSInteger status = [(NSHTTPURLResponse*)response statusCode];
if(error){
NSLog(#"http request error: %#", error.localizedDescription);
// handle the error
}
else{
if (status == 200){
// handle the success
}
else{
NSLog(#"error");
} // handle the error
}
}];
[1]: https://i.stack.imgur.com/FAMJy.png ---This is the image in the server
[1]: https://i.stack.imgur.com/7NGSK.png --- This is the actual image that I want
you should remove orientation data from UIImage before uploading to the server.
UIImage *originalImage = [requestBody objectForKey:keyUploadImage];
UIImage *imageToUpload =
[UIImage imageWithCGImage:[originalImage CGImage]
scale:[originalImage scale]
orientation: UIImageOrientationUp];
You should use imageToUpload to upload to the server.
the image rotation is happening when I crop the image then I used this https://stackoverflow.com/a/14231694/7636194 after cropping then my issue is got fixed.
I can't upload images to Cloudinary hosting via HTTPS POST request, I want to use the simple API methods instead of the SDK. I'm having a problem with formatting the image in a byte array buffer or as Base64 encoded.
Here's my code:
UIImage *image = [UIImage imageNamed:#"image.png"];
NSData *imageData = UIImagePNGRepresentation(image);
NSString *strImageData = [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
NSURL *url = [NSURL URLWithString:#"https://api.cloudinary.com/v1_1/MYSECTER/image/upload"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = #"POST";
NSString *strRequest = [NSString stringWithFormat:#"file=%#&upload_preset=MYSECTER", strImageData];
request.HTTPBody = [strRequest dataUsingEncoding:NSUTF8StringEncoding];
[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSDictionary *recievedData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
NSLog(#"RECEIVED: %#", recievedData);
}] resume];
Unfortunately I receive the following answer from the server: "Unsupported source URL..."
I really tried a lot of other methods, but I can't get it to work.
UPDATE: When I put URL link at 'file' param all works fine.
I don't have any experience with the Cloudinary API but I hope this is helpful. I had a look at the docs on 'Uploading with a direct call to the API'.
I think you are required to provide the 'file' and 'upload_preset' as POST parameters (not concatenated as data in the body).
UIImage *image = [UIImage imageNamed:#"image.png"];
NSData *imageData = UIImagePNGRepresentation(image);
NSString *strImageData = [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
NSURL *url = [NSURL URLWithString:#"https://api.cloudinary.com/v1_1/MYSECTER/image/upload"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = #"POST";
// Replace this:
//NSString *strRequest = [NSString stringWithFormat:#"file=%#&upload_preset=MYSECTER", strImageData];
//request.HTTPBody = [strRequest dataUsingEncoding:NSUTF8StringEncoding];
// With this:
NSString *imageDataStr = [[NSString alloc] initWithData:imageData encoding:NSUTF8StringEncoding];
[request setValue:imageDataStr forHTTPHeaderField:#"file"];
[request setValue:#"MYSECTER" forHTTPHeaderField:#"upload_preset"];
[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSDictionary *recievedData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
NSLog(#"RECEIVED: %#", recievedData);
}] resume];
You can use Cloudinary's SDK in order to perform an upload image task as explained here: http://cloudinary.com/blog/direct_upload_made_easy_from_browser_or_mobile_app_to_the_cloud
The following code samples show a direct unsigned upload API call in Objective-C for iOS...:
CLCloudinary *cloudinary = [[CLCloudinary alloc] init];
[cloudinary.config setValue:#"demo" forKey:#"cloud_name"];
NSString *imageFilePath = [[NSBundle mainBundle] pathForResource:#"logo"
ofType:#"png"];
CLUploader* uploader = [[CLUploader alloc] init:cloudinary delegate:self];
[uploader unsignedUpload:imageFilePath uploadPreset:#"zcudy0uz" options:#{}];
The following code samples show a more advanced example: specifying a custom public ID of user_sample_image_1002, making it possible to later access the uploaded image, and assigning a tag to simplify management of the images. In addition, we show an example of building a dynamic URL that performs an on-the-fly image manipulation: generating a 150x100 face-detection-based thumbnail of the uploaded images for embedding in your application.
NSData *imageData = [NSData dataWithContentsOfFile:imageFilePath];
[uploader unsignedUpload:imageData uploadPreset:#"zcudy0uz" options:
[NSDictionary dictionaryWithObjectsAndKeys:#"user_sample_image_1002",
#"public_id", #"tags", #"ios_upload", nil] withCompletion:^(NSDictionary
*successResult, NSString *errorResult, NSInteger code, id context) {
if (successResult) {
NSString* publicId = [successResult valueForKey:#"public_id"];
NSLog(#"Upload success. Public ID=%#, Full result=%#", publicId,
successResult);
CLTransformation *transformation = [CLTransformation transformation];
[transformation setWidthWithInt: 150];
[transformation setHeightWithInt: 100];
[transformation setCrop: #"fill"];
[transformation setGravity:#"face"];
NSLog(#"Result: %#", [cloudinary url:publicId
options:#{#"transformation":
transformation, #"format": #"jpg"}]);
} else {
NSLog(#"Upload error: %#, %d", errorResult, code);
}
} andProgress:^(NSInteger bytesWritten, NSInteger totalBytesWritten,
NSInteger totalBytesExpectedToWrite, id context) {
NSLog(#"Upload progress: %d/%d (+%d)", totalBytesWritten,
totalBytesExpectedToWrite, bytesWritten);
}];
I found solution, and it works very good:
// image for sending
UIImage *image = [UIImage imageNamed:#"image.png"];
// Dictionary that holds post parameters. You can set your post parameters that your server accepts or programmed to accept.
NSMutableDictionary* _params = [[NSMutableDictionary alloc] init];
[_params setObject:#"SECKET_PRESET" forKey:#"upload_preset"];
// the boundary string : a random string, that will not repeat in post data, to separate post data fields.
NSString *BoundaryConstant = #"----------V2ymHFg03ehbqgZCaKO6jy";
// string constant for the post parameter 'file'. My server uses this name: `file`. Your's may differ
NSString* FileParamConstant = #"file";
// the server url to which the image (or the media) is uploaded. Use your server url here
NSURL* requestURL = [NSURL URLWithString:#"https://api.cloudinary.com/v1_1/SECKET_KEY/image/upload"];
// create request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:30];
[request setHTTPMethod:#"POST"];
// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", BoundaryConstant];
[request setValue:contentType forHTTPHeaderField: #"Content-Type"];
// post body
NSMutableData *body = [NSMutableData data];
// add params (all params are strings)
for (NSString *param in _params) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#\r\n", [_params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}
// add image data
NSData *imageData = UIImagePNGRepresentation(image);
if (imageData) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"; filename=\"image.jpg\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:#"--%#--\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// set the content-length
NSString *postLength = [NSString stringWithFormat:#"%lu", (unsigned long)[body length]];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
// set URL
[request setURL:requestURL];
[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSDictionary *recievedData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
NSLog(#"RECEIVED: %#", recievedData);
}] resume];
I send an image to a server HTTP with this code:
-(NSDictionary*)sendImage:(UIImage*)image withDescription:(NSString*)description andTarget:(NSString*)target andTargetId:(int)targetId {
__block NSDictionary *result = nil;
NSData *imageData = UIImagePNGRepresentation(image);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#%#", PREFIX_URL_REQUEST, SEND_IMAGE]]];
[request setHTTPMethod:REQUEST_TYPE_POST];
NSString *boundary = [[NSString alloc] init];
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[request addValue: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=\"file\"; filename=\"%#.%d\"rn", target, targetId] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Type: application/%#.png\r\n\r\n", [NSString stringWithFormat:#"%#_%d", target, targetId]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
dispatch_semaphore_t sem = dispatch_semaphore_create(0);
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
result = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(#"%#", result.description);
dispatch_semaphore_signal(sem);
}];
[dataTask resume];
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
return result;
}
this is the server code:
session_start();
if ( isset($_SESSION['username']) ) {
$params = explode(".", basename($_FILES["file"]["name"]));
$target = array_values($params)[0];
$id = array_values($params)[1];
if ( ($target == "profile") || ($target == "question" && $_SESSION["waitingImagesQuestion"] > 0) || ($target == "answer" && $_SESSION["waitingImagesAnswer"] > 0) ) {
$imageDirectory = "images/".round(microtime(true)*1000).".png";
if ( move_uploaded_file($_FILES["file"]["tmp_name"], $imageDirectory) ) {
switch ( $target ) {
case "profile":
print JsonBuilder::putRecordProfileImage($imageDirectory);
return;
case "answer":
$_SESSION["waitingImagesAnswer"] --;
print JsonBuilder::putRecordAnswerImage($imageDirectory, $id);
return;
case "question":
$_SESSION["waitingImagesQuestion"] --;
print JsonBuilder::putRecordQuestionImage($imageDirectory, $id);
return;
}
}
}
}
print JsonBuilder::getStringError();
works, but only when I call the client method so ([UIImage imageNamed:#"aaa.png"]):
[sender sendImage:[UIImage imageNamed:#"aaa.png"] withDescription:#"" andTarget:#"profile" andTargetId:1];
if I call the method with an UIImage (for example a photo taken from the camera roll), so:
-(void)sendImage:(UIImage*)image {
return [sender sendImage:image withDescription:#"" andTarget:#"profile" andTargetId:1];
}
doesn't work!
Why?
Thanks
now it works:
-(NSDictionary*)sendImage:(UIImage*)image withDescription:(NSString*)description andTarget:(NSString*)target andTargetId:(int)targetId {
if ( image != nil ) {
NSData *imageData = UIImageJPEGRepresentation(image, 0.33f);
__block NSDictionary *result = nil;
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#%#", PREFIX_URL_REQUEST, SEND_IMAGE]]];
[request setHTTPMethod:REQUEST_TYPE_POST];
NSString *boundary = #"0xKhTmLbOuNdArY";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[request addValue: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=\"file\"; filename=\"%#.%d\"rn", target, targetId] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Type: application/%#.png\r\n\r\n", [NSString stringWithFormat:#"%#_%d", target, targetId]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
dispatch_semaphore_t sem = dispatch_semaphore_create(0);
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
result = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
dispatch_semaphore_signal(sem);
}];
[dataTask resume];
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
return result;
}
return nil;
}
I am trying to migrate to NSURLSession so I can send multiple images to my server and stop when I get the needed response. But my completion block never gets called. Would appreciate if you could tell me if I am doing the migration correctly.
Here is my old code that works fine-
-(void) sendImgWithText:(UIImage*)img
{
NSURL *requestURL = [NSURL URLWithString:#"Some URL"];
// create request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:30];
[request setHTTPMethod:#"POST"];
NSString *boundary = #"*****";
NSString *lineEnd = #"\r\n";
NSString *twoHyphens = #"--";
// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[request setValue:contentType forHTTPHeaderField: #"Content-Type"];
// post body
NSMutableData *body = [NSMutableData data];
// add params (all params are strings)
// UIImage *imgColor = [UIImage imageNamed:#"9.jpg"];
UIImage * imageToPost = [[UIImage alloc] init];
UIImageWriteToSavedPhotosAlbum(imageToPost, nil, nil, nil);
NSData *imageData = UIImageJPEGRepresentation(imageToPost, 1.0);
if (imageData) {
[body appendData:[[NSString stringWithFormat:#"%#%#%#", twoHyphens,boundary, lineEnd] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"test.jpg\"%#",lineEnd] dataUsingEncoding:NSUTF8StringEncoding]];
// [body appendData:[#"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#",lineEnd] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:#"%#",lineEnd] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:#"%#%#%#%#", twoHyphens,boundary, twoHyphens,lineEnd] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// set the content-length
NSString *postLength = [NSString stringWithFormat:#"%d", [body length]];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
NSLog(#"%#",requestURL);
NSLog(#"%f,%f",imageToPost.size.height,imageToPost.size.height);
// set URL
[request setURL:requestURL];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
And here is the version(I got most of it from other stack overflow articles)
- (void) serverRequestWithImage:(UIImage *)img completion:(void (^)(id responseObject, NSError *error))completion
{
NSURL *requestURL = [NSURL URLWithString:#"Some URL"];
// create request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:30];
[request setHTTPMethod:#"POST"];
NSString *boundary = #"*****";
NSString *lineEnd = #"\r\n";
NSString *twoHyphens = #"--";
// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[request setValue:contentType forHTTPHeaderField: #"Content-Type"];
// post body
NSMutableData *body = [NSMutableData data];
UIImage * imageToPost = [[UIImage alloc] init];
imageToPost = img;
NSData *imageData = UIImageJPEGRepresentation(imageToPost, 1.0);
if (imageData) {
[body appendData:[[NSString stringWithFormat:#"%#%#%#", twoHyphens,boundary, lineEnd] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"test.jpg\"%#",lineEnd] dataUsingEncoding:NSUTF8StringEncoding]];
// [body appendData:[#"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#",lineEnd] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:#"%#",lineEnd] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:#"%#%#%#%#", twoHyphens,boundary, twoHyphens,lineEnd] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// set the content-length
NSString *postLength = [NSString stringWithFormat:#"%d", [body length]];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
NSLog(#"%#",requestURL);
NSLog(#"%f,%f",imageToPost.size.height,imageToPost.size.height);
// set URL
[request setURL:requestURL];
NSURLSessionTask *task =
[session
dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
// report any network-related errors
NSLog(#"Got Response 1");
if (!data) {
if (completion) {
dispatch_async(dispatch_get_main_queue(), ^{
completion(nil, error);
});
}
return;
}
// report any errors parsing the JSON
NSError *parseError = nil;
_responseData = [NSMutableData dataWithData:data];
if (_responseData) {
if (completion) {
dispatch_async(dispatch_get_main_queue(), ^{
completion(nil, parseError);
});
}
return;
}
// if everything is ok, then just return the JSON object
if (completion) {
dispatch_async(dispatch_get_main_queue(), ^{
completion(_returnedData, nil);
});
}
}];
[task resume];
}
And here is my call method from the other class
- (IBAction)GetData:(id)sender
{
[_imageView setImage:[UIImage imageNamed:#"9.jpg"]];
msg = [[Message alloc] init];
[msg initData];
msg.delegate=self;
[msg serverRequestWithImage:_imageView.image completion:^(id responseObject, NSError *error)
{
if (responseObject) {
// do what you want with the response object here
NSLog(#"Got Data");
} else {
NSLog(#"%s: serverRequest error: %#", __FUNCTION__, error);
}
}];
}
Items:
I didn't see the initialization of NSURLSession. That's something you should show in your question. You could also check in your "send image" method that session is non-nil.
It looks like you are starting with a file (#"9.jpg"). If so, -[NSURLSession uploadTaskWithRequest:fromFile:completionHandler:] might save you a bunch of trouble.
Unrequested advice, with all that entails: :)
In my view, -serverRequestWithImage:completion: is a poor name for that method. It implies that it returns a request object, and does not tell you that it actually sends the request. Something active, like -uploadImage:completionHandler: might be better.
Idiomatically, all method names should start lowercase, i.e. -getData:.