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" :)
Related
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.
I have created code which takes a string from a text field and calls a encrypting method for doing encryption of that text using the base64 encoding scheme. But I don't have any idea how to create the decryption method for decrypting whatever value I am getting from the method.
Save yourself some trouble and download the NSData Base64 category.
Then you can encode or decode using a single line.
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 :)
I built an iPhone app that is getting information from a server (this is also a server that I built).
The data from the server is XML and I use the XML parser to parse the message.
What I want is to add an image to be sent from the server, and I am asking if I can add binary data of such an image to the XML message. For example 10 tags will be text and 1 tag will be binary (the image). So when the XML parser gets to the binary tag, it inserts the data to NSDATA object and the rest of the tags will be inserted to NSString.
Does the XML parser of Cocoa can handle this situation?
If not, what do you think will be the easiest way to do this with one connection to the server so that the data from the server is sent once.
To transfer binary data wrapped in XML, encode it using e.g. Base64, which turns your binary data into characters that won't mess up your XML.
You can transfer the image data, encoded using Base64. There is this NSData category by Matt Gallagher that adds Base64 decoding support to NSData (dateFromBase64String). You can find it on his Cocoa with love website.
Mind you that encoding images in Base64 adds about 33% in file size.
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