Johnykutty
Johnykutty

Reputation: 12829

Base64 decoding issue with image data-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

Upvotes: 2

Views: 8579

Answers (1)

djromero
djromero

Reputation: 19641

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

Upvotes: 3

Related Questions