Displaying base64 encoded image in rails - ios

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>

Related

Due to some base64 character i am not able to upload image in ios

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];

Why [UIImage imageWithData:] supports PVRTC format?

I suddenly find out that in iOS 8.3 SDK (I didn't test other versions), I can use [UIImage imageWithData:] to load PVRTC format directly?
It isn't supposed to be like that, right? I can't find any documentation or discussion about it, I don't even know if I can rely on it...
But it does work in both simulators and real devices.
Here are some codes of my test, the test project doesn't include OpenGLES.framework.
NSData *data = THE_CONTENT_OF_A_PVR_FILE;
NSString *tempDir = NSTemporaryDirectory();
[data writeToFile:[tempDir stringByAppendingPathComponent:#"temp.pvr"] atomically:YES];
UIImage *tempImage = [UIImage imageWithData:data];
[UIImagePNGRepresentation(tempImage) writeToFile:[tempDir stringByAppendingPathComponent:#"temp.png"] atomically:YES];
The two saved files are shared here: http://d.pr/f/17ugQ
You can check the pvr file in a HEX editor, it coresponds to the specifications of PVRTC format version 2, with pixel format in PVRTC4.
Any idea?

UIImage Base64 Encoding and web service issue - IOS

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.

sending images to server via json

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.

Compare two UIImages with CC_MD5 in iOS ,but the hash code will be changed after reboot the device

I 've use
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(inImage)];
CC_MD5(imageData, [imageData length], result);
for generate MD5 code of my picture and add them to NSDictionary for compare the image from photoLibrary.
But when I add some code or reboot my device , I find the hash code is totally changed .
How can resolve this problem ?
You should use
NSData *imageData = [NSData dataWithContentsOfFile:file];
CC_MD5(imageData, [imageData length], result);
that must work fine.
Also consider to use sha1 as basing algorithm.
PNG file format allows to have timestamps inside the binary data. This will be different every time you call UIImagePNGRepresentation. I am afraid you can't use MD5 to compare the data of two images this way. Try to extract the raw image data from the files and generate MD5 of the raw data.

Resources