I would like to post image to a webservice with the format below:
picture=%89PNG%0D%0A%1A%0A%00%00%00%0DIHDR%00%00%00%01%00%00%00%01%01%03%00%00%00%25%DBV%CA%00%00%00%04gAMA%00%00%B1%8F%0B%FCa%05%00%00%00%01sRGB%00%AE%CE%1C%E9%00%00%00+cHRM%00%00z%26%00%00%80%84%00%00%FA%00%00%00%80%E8%00%00u0%00%00%EA%60%00%00%3A%98%00%00%17p%9C%BAQ%3C%00%00%00%06PLTE%11%0B%0C%FF%FF%FFPU%C0J%00%00%00%01tRNS%94%B7%84%8F%3B%00%00%00%01bKGD%01%FF%02-%DE%00%00%00%09pHYs%00%00%00H%00%00%00H%00F%C9k%3E%00%00%00%0AIDAT%08%D7c%60%00%00%00%02%00%01%E2%21%BC3%00%00%00%25tEXtdate%3Acreate%002013-01-06T12%3A31%3A53%2B02%3A00%92R%3A%D3%00%00%00%25tEXtdate%3Amodify%002013-01-06T12%3A31%3A53%2B02%3A00%E3%0F%82o%00%00%00%19tEXtSoftware%00Adobe+ImageReadyq%C9e%3C%00%00%00%00IEND%AEB%60%82
My problem is how to convert an image to this format.
Thanks to any help.
I found a solution. If someone need it:
NSData *imageData = UIImagePNGRepresentation(self.photoImageView.image);
NSString *imageStringASCII = [[NSString alloc] initWithData:imageData encoding:NSASCIIStringEncoding];
convert your image into Base64 i.e., encode with help of the following library of Base64
and upload image to server . In te server decode and convert into image and store
Related
I am uploading a image on a local server but when i am trying to upload on live url that images not convert in proper formate might be.
The php file is same. where i am trying to upload image. I think due to some base64 character not able to uploading image. please suggest me any solutions for base64 character problem.
I also face the same problem. So i replace #"+" with #"%2B". May be it is also helpful for you.
NSString* result = [base64EncodedImage stringByReplacingOccurrencesOfString:#"+" withString:#"%2B"];
Try This
NSData *imageData = UIImageJPEGRepresentation(profilePhotoObj.image, 1.0);
NSString *base64Img = [imageData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
I have send an image using Base64 ENCODING to REST web service but when opened at web services end, it is shown black?
My code is shown below:
UIImage *image1;
NSString *encodedstring;
NSData *imagedata =UIImageJPEGRepresentation(image1, 1.0f);
encodedstring = [imagedata base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
Why don't you initiate image1 with image name. Seems like image1 doesn't contain any image.
For your test, try:
UIImage *image1 = [UIImage imageNamed:#"some image file name"];
You may want to NSLog encodedstring and test it here: http://www.freeformatter.com/base64-encoder.html So you can see base64 string for image is actually converted right before you send to sever.
I solved this with URL encoding + with %2B while sending the string. Replacing + with %2B would help. Your server side code will automatically replace %2B as +
Actually what happens here is when we try to send the base64 string using URL connection, Each + in the string is replaced with a blank space. This makes the string invalid and thus resulting in black image.
Hope this helps! Cheers.
Update:
I took the base64 string iOS was generating and decoded it to a binary file through a website. The image was encoded properly it seems.
My problem then is with the rails side of things. I'm displaying the image using the following code but it's just showing a broken image.
<td><%= ('<img src="data:image/jpg;base64,%s">' % Base64.encode64(user.imagestring)).html_safe %></td>
I'm trying to encode a base64 string to send through JSON to my web service (Rails using Postgresql). I've converted a UIImage to NSData and am now converting it to a base64 string.
I'm using the new base64EncodedStringWithOptions method. It converts to a long string but on my web service the image appears broken.
I uploaded the image to an image to base64 converter website and it returns a slightly different string.
Is the problem with the iOS encoder or am I doing something wrong?
UIImage *image = [UIImage imageNamed:#"image.png"];
NSData *imageData = UIImagePNGRepresentation(image);
NSString *blob = [imageData base64EncodedStringWithOptions:0];
You are doing a round-trip through UIImage and UIImagePNGRepresentation(image) to get the data to be base 64 coded. That might not be the same contents as the original file. There are tons of options when coding images, and you're not assured that your roundtrip will use the same options.
I would, instead, suggest just loading the NSData directly:
NSString *path = [[NSBundle mainBundle] pathForResource:#"image" ofType:#"png"];
NSData *imageData = [NSData dataWithContentsOfFile:path];
Try running that through your base 64 coding and see what you get.
The problem was with the ruby code. This worked:
<td><%= ('<img src="data:image/png;base64,%s">' % user.image).html_safe %></td>
I am posting data to a server from my ipad using json. But along with my data, i need to be able to send images aswell. I tried to add my image data to a dictionary and parse that into json, but json doesnt like nscfdata. What would be the easiest way i can post my images to the server? from other posts related to this topic, people have been converting to base64. Would i have to do that, or is there another easier or faster way? if i have to encode to base64 is there any tutorial on that?
I convert it to base64. Check out this tutorial to get started!
http://www.cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html
Example:
NSImage *image = // some image
NSData *data = UIImagePNGRepresentation(image);
NSString *base64EncodedString = [data base64EncodedString];
You can then send the string via JSON.
What is the easiest and fastest code to do a conversion between NSData and a base64 string? I've read a bunch of solutions at SO and mostly they involve in adding another class etc. I found a great solution here but it's too complex.
Scroll down to the Conclusion section on the page you linked and download the provided NSData+Base64 files. Its the best solution I have seen so far and is incredibly easy to use. If you can learn anything about Cocoa, you can learn to use that project.
Example
NSString *originalString = [NSString stringWithFormat:#"test"];
NSData *data = [NSData dataFromBase64String:originalString];
NSLog([data base64EncodedString]);
The above will print out the original string after converting it to base64 and back to a normal unencoded string.
As of iOS 7, NSData now directly provides this functionality with the new methods -base64EncodedDataWithOptions: and -base64EncodedStringWithOptions:. (The options let you specify that the string is/should be line-wrapped, the better to deal with email, and user-facing displays.)
You don't need any custom implementation. Creating base64 from NSData is shown in other answers. There is opposite direction. From Base64 string to NSData:
NSString *base64Encoded = #"some base64 string";
NSData *nsdataFromBase64String = [[NSData alloc] initWithBase64EncodedString:base64Encoded options:0];
I ended up using this same class as provided by SUDZC
implementation was easy first I did an import
#import "NSData+Base64.h"
then I was able to call my data.
NSData *data = [[NSData alloc] initWithData:[NSData dataWithBase64EncodedString:strData]];
Be aware that there are more Base64 formats.
For example JWTs use a URL safe format.
Or you may take a look to the (quite new) CryptoCompatibility sample project, I think there is a wrapper class for base64 operation. It is a MacOS sample but it uses the library libresolve.dylib with I think is available on iOS too (is see it at least here in iOS7).