xcode 图像数据-iphone的Base64解码问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8341458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Base64 decoding issue with image data-iphone
提问by Johnykutty
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
我在从 base 64 编码字符串解码图像数据时遇到一些问题。我正在使用从以下链接下载的 base64.h 和 base64.m 文件
http://cdn.imthi.com/e6cef8/wp-content/uploads/2010/08/base64.zip
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
我通过使用在线 base 64 解码器解码 base 64 字符串(从调试器控制台获取)进行了测试,它提供了正确的图像,我还通过将数据写入这样的文件进行了测试
[data writeToFile:imagePath atomically:YES];
it gives a jpg file but i can't open that image file, it gives error message like
它给出了一个 jpg 文件,但我无法打开该图像文件,它给出了类似的错误消息
The file “test.jpg” could not be opened.
无法打开文件“test.jpg”。
"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.....
“它可能已损坏或使用了 Preview 无法识别的文件格式。” 我的代码有什么问题 谁能帮我.....
Thank you
谢谢
回答by djromero
Try a different base 64 implementation, I use the one from colloquyopen source project:
尝试不同的 base 64 实现,我使用来自colloquy开源项目的实现:
#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
获取NSAdditions
文件:NSAdditions.h+ NSAdditions.m