POST UIImage with multiple fields - iphone

Found plenty of code examples of how to POST a UIImage to a server that I can successfully do, having a little trouble trying to include fields in the request, in addition to the UIImage itself.
As an example, i'd like to POST the following..
UIImage *image = imageView.image;
NSString *userString #"user_id=5";
NSString *typeString #"type=7";
Thanks for your help. Like I said, sending a UIImage is fine, adding fields to the request is what i'm having trouble with.

You would have to do something like:
NSData *data = UIImageJPEGRepresentation(image, 1.0f);
The number in that call is the image compression quality (1.0 is no compression). Then you will need to get the string representation of this data. Then escape your string for inclusion in a URL. And finally base64 encode it. Then set the httpBody of your request to be the string, along with your other parameters.
If you need any further help with how to do this, let me know :)

Related

How to send a Message from iPhone to Windows?

I have an object with the following members:
NSString *reqStr = "param1=val1&param2=val2&param3=val3&..";
NSData *imageData = [NSJPEGRepresentation (myimage)];
NSData *fileContents = [NSData initWithFileContents(myfile.txt)];
How can I send this out to Windows WCF? Do I send it as stream of bytes, and attach to httprequest? Or, will this be sockets? I am not sure how to pack these things as one thing as in one stream of bytes or whatever it may be the way to format such an object.
Any help?
Since you said that you already know how to send imageData which is NSData. Why not convert regStr to NSData and combine all three together as a single data and send.
To convert NSString to NSData:
NSData* strData=[regStr dataUsingEncoding:NSUTF8StringEncoding];
And use NSMutableData's appendData method to combine all three.
NSMutableData *combineData = [[NSMutableData alloc]initWithData:strData];
[combineData appendData:imageData];
[combineData appendData:fileData];
This S/O question might help:
File Upload to HTTP server in iphone programming
If the WCF endpoint is configured as HTTP then the same principles should apply regarding the multipart/formencoding of the image in question.
Edit:
Is this answer more helpful ?
HTTP "POST" request in iOS

How to post uiimage to soap Srevice in binary formate

I want to post uiimage into soap service and input image is into binary formate
"<ExpenseDetailReceipt>%#</ExpenseDetailReceipt>"
and send this appdelegate.imgBMeal data
appdelegate.imgBMeal=UIImagePNGRepresentation(appdelegate.imgMeal);
as image but there is some problem and I get please send data in correct formate
please help me how to do this
Soap is basically an xml which is a string. Hence, your UIImagePNGRepresentation(appdelegate.imgMeal) method which does not return a string fails. The output of the UIIagePNGRepresentation() is an NSData object which you can covert to a string by using Base64 encoding. (You may use any other encoding of your choice). For more about coverting NSData to NSString using Base64 encoding, there is an SO link with many answers.. here. You may also need to check with your service provider about the encoding decoding parts.

UIImage Base64 encoding fails

i'm trying to convert a UIImage which comes from the iPhones photo library to a base64 encoded string. My problem is that the output of the base64 encoded string does not match a base64 string which i've created on a online base64 encoder for jpegs.
Here is my code:
NSData *myAttachment = UIImageJPEGRepresentation(myImage, 1.0);
NSString *base64encodedAttachment = [attachment base64EncodingWithLineLenght:76];
While "attachment" is a string which will be part of an url request.
Why does the base64 code not match the "normal" from encoding websites encoding? Is there something special about UIImage oder NSData?
Cheers
It may happen that the UIImage is re-encoded when you call UIImageJPEGRepresentation and therefore it is not identical to the JPEG image you have uploaded to that online site. If you want to be 100% sure that the base64 encoder works as expected, save the JPEG representation that comes out of UIImageJPEGRepresentation to a JPEG file and upload that file to the online tool to obtain the base64 representation to compare your output with.
If you are trying to use this jpeg file in the messageBody of the mailComposer.. then i must clear you buddy...
You cannot use the local images inside the mailbody of mailComposer, the image shud be available on the server and used in the mailbody by passing url of the same..
If you're trying something else...i would say follow "Tamas" :)

How do i transform a file(any type of file) in Base64 to send to a web service Objective C

I need to send a files to a XML web service. The web service only accepts the file in a base64 format and rebuilds it in the back end. I need to know how do i convert a file to base64 in Objective C.
Check below SO post
How do I do base64 encoding on iphone-sdk?
How can I decode data with Base64 in IPhone
How to Base64 encoding on the iPhone
UIGraphicsBeginImageContext(view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imagedata = UIImagePNGRepresentation(viewimage);
NSString *encodedString = [imageData base64Encoding];
where view is the view where lines are drawn.

Retrieving images to iphone app through XML

I have made an iphone application in which data is retrieved from remote server through xml saved. I want to retrieve images also with in the XMl and have to show them in my iphone application stored on server. Please help how this can be achieved.
In my application I am both sending and receiving images through Xml. In order to facilitate this, I encode/decode the images using base64 encoding. MY encode/decode methods live in 2 objective-c categories, 1 for NSData and 1 for NSString. You call these methods on the respective data types, I have listed examples below. The methods are [NSData base64Encoding] and [NSString base64Decoding].
When sending to the server...
NSLog(#"Compressing Image: JPEG 80%");
NSData *imgData = UIImageJPEGRepresentation(scaledImage, 0.8f);
NSString *b64String = [imgData base64Encoding];
My "b64String" variable now has a base64 encoded string representation of my image. You can now wrap this in Xml and use your normal NSURLConnection to send it to the server.
When receiving from the sever... (Make sure you are sending base64 encoded text from the server)
You will do your normal Xml parsing, here my "value" variable holds the base64 string from the server.
NSData *imgData = [value base64Decoding];
UIImage *image = [[UIImage alloc] initWithData:imgData];
You should be able to look up some common base64 encoding/decoding algorithms in Objective-C, let me know if you would like me to post my methods (they are kinda long so I obmitted them here).
I'm writing an app at the moment that has this problem - I've solved it by putting a url to the image in the xml instead of embedding the image data itself. For example my xml looks something like this :
<images>
<image url="http://www.example.com/images/12345.jpg" />
<image url="http://www.example.com/images/67890.jpg" />
</images>
As I'm parsing the xml, when I hit an < image > tag I store the url in an NSMutalbeArray. When the xml is parsed I loop through the array and get each image using NSURLConnection.
Sam
PS I worked out how to use NSURLConnection from this page : https://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html