1

Is it possible to convert NSData/UIImage Data Representation as JPEG to a String, to be sent over HTTP to a PHP File to save this string in a database, and then retrive it later on in the application and convert it back into an NSData/UIImage Object?

I have tried Base64 Encoding Libraries but base64 doesn't seem valid as the image doesn't display correctly on a HTML Page.

Any suggestions?

Edit.

I was using the following library:

http://www.imthi.com/blog/programming/iphone-sdk-base64-encode-decode.php

And converting in the following way:

 NSData *imageData = UIImageJPEGRepresentation(MyImage.image, 90);
[Base64 initialize];
NSData *encoded = [Base64 encode:imageData];
NSLog(@"%@",encoded);

This does chug out alot of BASE64 but when I save it to a file and try to view it, I just get the eror loading image [?] in Chrome.

Thanks

6
  • 2
    Base64 works fine. In fact, when you do inline HTML images, it uses Base64. There must be something wrong with your attempts. Give it another go and if you don't succeed, update your question with your code and we'll take a look at it. Commented Feb 10, 2013 at 16:39
  • Updated the above post Commented Feb 10, 2013 at 16:54
  • as a side note: don't store the img data as a string. b64 is fine for transfer but it is bad for storage Commented Feb 10, 2013 at 17:04
  • The object that the "encoded" pointer references should be NSString, not NSData. Commented Feb 10, 2013 at 17:10
  • Direct convert image to NSData. We can use like that i have answered. Commented Feb 10, 2013 at 17:48

3 Answers 3

3

The point of encoding an NSData object to base 64 is so you can represent the data as a string that can be stored or transferred more easily. You then need to decode the base 64 encoded string back into NSData. This data can then be used to create a new UIImage. Your server needs to do this decoding to get back the original data.

Your code has a mistake. This line:

NSData *encoded = [Base64 encode:imageData];

should be:

NSString *encoded = [Base64 encode:imageData];

Notice that you get back a string, not data.

You commented that you wrote the encoded string to a file then couldn't view the image. Of course not. If you want to write the image data to a file so the file is actually viewable as the image, then don't encode the data first. Write the raw image data to a file.

Sign up to request clarification or add additional context in comments.

Comments

-1

you can convert image to string like this first convert your UIImage to NSData & then convert that ata into string by using encodeBase64WithData

NSString *imageOne = [self encodeBase64WithData:[imageDict objectForKey:@"ImageOne"]];

and again string to UIImage like this:

[UIImage imageWithData: [self decodeBase64WithString:[registerDataDict objectForKey:@"imageOne"]]];

2 Comments

it contained NSData ,which comes after converting UIImage into NSData
I have tried to impliment this but I get the error "No Visible @interface for 'NSData' declares the selector 'objectForKey' Implimentation: NSData *imageData = UIImageJPEGRepresentation(MyImage.image, 90); NSString *imageOne = [self encodeBase64WithData:[imageData objectForKey:@"ImageOne"]];
-1

You need to import Base64.h

You can directly use like this way:

 UIImage *image = _btnaddCountryIcon.imageView.image;
 NSData *imageData = UIImagePNGRepresentation(image);
 NSString *base64 = [Base64 encode:imageData];

directly you can convert to NSString. This code works fine for me.

7 Comments

This doesn't answer the question. The OP already stated that they get the encoded string already. All you have done is reposted the same code.
i am using this thing for uploading image. It's common stuff.
That's not the point. Your "answer" doesn't answer the question. The OP already knows how to get the base 64 string from the image data.
Because what i understand is that he is not able to load data to string so i have given the conversion.
If you read the question, it is clear that he doesn't know what to do with the base 64 string. The question is all about what do after getting the result shown in your answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.