Base64 decoding issue with image data-iphone - iphone

I have some problem for decoding image data from base 64 encoded string.
I am using base64.h and base 64.m files downloaded from the following link
http://cdn.imthi.com/e6cef8/wp-content/uploads/2010/08/base64.zip
This is my code
[Base64 initialize];
NSData * data = [Base64 decode:imageString];
imgview.image=[UIImage imageWithData:data];
but, nothing displayed in the image view ,
I tested by decoding the base 64 string(taken from debugger console) with an online base 64 decoder,It gives correct image,
I also tested by writing the data to a file like this
[data writeToFile:imagePath atomically:YES];
it gives a jpg file but i can't open that image file,
it gives error message like
The file “test.jpg” could not be opened.
"It may be damaged or use a file format that Preview doesn’t recognize."
What is the problem with my code
Can anyone help me.....
Thank you

Try a different base 64 implementation, I use the one from colloquy open source project:
#import "NSDataAdditions.h"
/* encoded string to image */
NSString *imageString = #"";
NSData * data = [NSData dataWithBase64EncodedString:imageString];
UIImage *image1 = [UIImage imageWithData:data];
/* image to encoded string, back to image */
imageString = [UIImagePNGRepresentation(image) base64Encoding];
data = [NSData dataWithBase64EncodedString:imageString];
UIImage *image2 = [UIImage imageWithData:data];
Get NSAdditions files: NSAdditions.h + NSAdditions.m

Related

NSString to NSData to UIImage and vice versa conversions

I am able to get the base 64 string of an image using the following code,
UIImage *thumbImage = [UIImage imageNamed:#"image.png"];
NSData *theThumbImageData = UIImageJPEGRepresentation(thumbImage,0.1);
NSString *str = [theThumbImageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
Now i am trying to get the image back from base64 string and below code is used for that purpose.
NSString *string = #"base64 encoded string retrieved from server";
NSData *base64Data = [imageData dataUsingEncoding:NSUTF8StringEncoding];
UIImage* theMultimediaImage = [[UIImage alloc] initWithData:[base64Data base64EncodedDataWithOptions:0]];
The problem am facing here is that during the image conversion from base64 string am getting NIL value.
Please help me to rectify my mistake.
Note: Followed the link ,Converting NSString to NSData and vice versa
Regards,
Bhat
In the first line of code you are initializing an NSString variable named 'string' but in the 2nd line of code you are trying to extract data from a different (presumably string) variable named 'imageData'.

NSData/UIImage to String

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

IPhone NSData to UIImage

my NSData object contains within byte of a photo.
I would like to convert this object to a UIImage.
My problem is that he could not give a name all'oogetto UIImage. example myphoto.jpg
this is my code:
NSString * bytestring =(NSString *)[bean image];
//contain byte of my photo
NSData * byte = [bytestring dataUsingEncoding:NSUTF8StringEncoding];
UIImage *image=[[UIImage alloc]initWithData:byte];
UIButton *bt=[[UIButton alloc]initWithFrame:CGRectMake(80,80,100,100)];
[bt setImage:image forState:UIControlStateNormal];
[self.boxView addSubview:bt];
the button does not display my photos.
how can I do?
Since it appears that [bean image] is actually a base 64 encoding of the image's data, you need to use a method to convert the encoded data back into unencoded data.
There is no standard method for this but there are plenty you can find and use. Your code then becomes something like this:
NSString *byteString = [bean image]; // no need for a cast if it's already an NSString
NSData *byte = // call some method to convert base 64 encoded string into an NSData object
Have a look at:
convert base64 decoded NSData to NSString

Get image from service URL in iPhone?

I have to retrieve image from rest service url.But I don't have a separate URL for image(path of the image).I m getting it in a XML file.I did parsing of XML and stored it in an array.Now I couldn't understand how to retrieve image from it ..
My array looks like this:
(
{
Image = "
9j/4AAQSkZJRgABAQAAAQABAAD//gA7Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcgSlBFRyB2NjIpLCBxdWFsaXR5ID0gODAK/9sAQwAGBAUGBQQGBgUGBwcGCAoQCgoJCQoUDg8MEB
cUGBgXFBYWGh0lHxobIxwWFiAsICMmJykqKRkfLTAtKDAlKCko/9sAQwEHBwcKCAoTCgoTKBoWGigoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgo/8AAEQgEKwZAAwEiAAIRAQMR
Af/EAB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC//EALUQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1RV
VldYWVpjZGVmZ2hpanN0dXZ3eHl6g4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2drh4uPk5ebn6Onq8fLz9PX29/j5+v/EAB8BAAMBAQEBAQEBAQEAAAAAAAABAgMEBQYHCAkKC//EALURAAIBAgQEAwQHBQQEAAECdwABAgMRBAUhMQYSQVEHYXETIjKBCBRCkaGxw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Bk9qOT9K2tNrGtepra02miWcptNOANSCNvSnbG9KmxdyIA04A1IEb0pQp9KLBcYM0vNSBT6UoU+lFguR804Zp+0+lGPaiwXGgmlyacF9qXHtRYLjQTS5NOx7UuPaiwCZNKGpQPalA9qLAIGpQ1LgUYosAbqdupMUuBQFwDU7NJijFAXF3Uu6kxRigLjt1LupuKMUBceDRupuKMUwuSbqN1MxS4oEP3UbqZilwaAH5pc0zFGDQA/dRupmDRzQA/dS7qZzRzQA/dRupnNHNAEm6jdTOaM0ASbqN1R0ZoAk3UbqjzRmgCTdRuqPNGaAJN1G6o80ZoAk3UbqZmkzQBLuo3VFmlzQBJuo3VHmjNAEu6jdUWaM0AS7qN1RZo3UAS7qN1RbqTdQBNuo3VDuozQBNuo3VDmjdQBNuo3VDuo3Uhku6jdUO6jdQBNuo3VDupN1AE26jdUO6jdQBNuo3VDuo3Ug
//big array </Image>
}
)
I couldn't understand what format(byte array or base64?) is this coming from service?
How to retrieve image from this in a tableViewCell?
If this is base64 string then You can decode base64 string to NSData using this and you can access UIImage from NSData.
+ (NSData *)decodeBase64WithString:(NSString *)strBase64;
This format is base64 then you should decode this string into data and get image. Use nsdata+ base64 class and import those class in your project.
Use + (NSData *)decodeBase64WithString:(NSString *)strBase64; method in your code. call like this
NSData *imageData = [NSData decodeBase64WithString:strBase64];
Edit:
NSString *yourBase64String = [yourDictionary valueForKey:#"Image"];
NSData *imageData = [NSData decodeBase64WithString:yourBase64String];
UIImage * image = [UIImage imageWithData:imageData];
May be it is because there were modifications to the NSData+Base64 files but the method now is dataFromBase64String instead of decodeBase64WithString

UIImage from bytes held in NSString

I am trying to create a UIImage from a byte array that is actually held within a NSString.
Can someone please tell me how I can do that?
Here is what I was thinking of doing:
NSString *sourceString = #"mYActualBytesAREinHERe=";
//get the bytes
const char *bytesArray = [sourceString cStringUsingEncoding:NSASCIIStringEncoding];
//build the NSData object
NSData *data = [NSData dataWithBytes:bytesArray length:[sourceString length]];
//create actual image
UIImage *image = [UIImage imageWithData:data];
The problem is image is always 0x0 (nil).
Any suggestions would be appreciated.
Thanks!
To convert an image to string you need a method to convert NSData to a base64Encoded string and back (lots of examples here). The easiest ones to use are categories on NSData so you can do something like this:
UIImage* pic = [UIImage imageNamed:#"sample.png"];
NSData* pictureData = UIImagePNGRepresentation(pic);
NSString* pictureDataString = [pictureData base64Encoding];
To go the other way you need a reverse converter:
UIImage* image = [UIImage imageWithData:[NSData
dataFromBase64EncodedString: pictureDataString]];
[UIImage imageWithData:data]; will return nil if it doesn't understand the data being passed to it. I would double check your encoding, etc. It's odd that a binary string would hold pure image data without some kind of encoding (base64, etc.). String encodings and binary encodings aren't compatible.
I bet your image data has some null characters in there (0x00) and as you know that is the terminator for the string, so when you ask for the C string, you probably get way-way too little data.
Try something like
- (NSData *)dataUsingEncoding:(NSStringEncoding)encoding;
to generate your NSData.
If that doesn't work you need to evaluate whether the setting the data into an NSString (with embedded null chars) isn't causing a loss of data too.
Like one of the other respondents, perhaps base-64 encoding your data would be a good idea (if using a string to transport the img data is a requirement)
Good luck.