Base64 decoding issue with image data-iphone

十年热恋 提交于 2019-12-08 03:56:40

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/8341458/base64-decoding-issue-with-image-data-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!