在Base64编码返回iPhone不同的结果比其他工具 [英] Base64 in iPhone returning different result than other tools

查看:639
本文介绍了在Base64编码返回iPhone不同的结果比其他工具的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为Base64 EN code上传iOS上的图像,但愚蠢的事情没有内置支持,于是我下载了两个库,并试图他们两个,但我认为他们正在返回错误的值(除非我理解的base64甚至不到我觉得我做的)。 网站会返回一个长字符串启动与此: / 9J / 4Q + zRXhpZgAASUkqAAgAAAAKAA4BAgAgAAAAhgAAAA8BAgAFAAAApgAAABABAgAKAA
当我使用加密我下载库,它开始与此: iVBORw0KGgoAAAANSUhEUgAAAI0AAABnCAIAAACy41YWAABAAElEQVR4AbzdeaylyXUY9nf3
我100%肯定,第一个是正确的,因为当我使用的是书面测试服务器测试程序,这是相同code,它上传到服务器,服务器高高兴兴地去codeS文件并保存图像。
这里是测试图像。

I'm trying to base64 encode an image for upload on iOS, but the stupid thing doesn't have built in support, so I downloaded two libraries and tried them both out, but I think they are returning the wrong values (unless I understand base64 even less than I think I do). This website returns a long string that starts with this: /9j/4Q+zRXhpZgAASUkqAAgAAAAKAA4BAgAgAAAAhgAAAA8BAgAFAAAApgAAABABAgAKAA When I encrypt using the library I downloaded, it starts with this: iVBORw0KGgoAAAANSUhEUgAAAI0AAABnCAIAAACy41YWAABAAElEQVR4AbzdeaylyXUY9nf3 I am 100% sure the first one is correct, because when I use a test program that was written to test the server, that's the same code that it uploads to the server and the server happily decodes the file and saves the image. Here is the test image.

基本上,我在想什么是我需要为iPhone / iPad的一个不同的库。我注意到另一个奇怪的是,对于一些图像,=将在字符串的结尾,和他人也不会。有三个原因,我可以为这个想了。

Basically, what I'm thinking is that I need to get a different library for the iPhone / iPad. Another odd thing I noticed is that for some images, the = will be at the end of the string, and for others it won't be. There are three reasons I can think of for this.

1)iPhone正在运行的内存并不想告诉我,所以它只是上传它有什么。

1) the iPhone is running out of memory and doesn't want to tell me so it just uploads what it has.

2)的库被破坏

3)我很困惑,不明白的base64

3) I'm confused and don't understand base64

有什么建议?

编辑:iPhone实际上是模拟器,所以我不知道这是否会运行的计算机确实出内存。我知道Android模拟器没有,但我不能确定的iOS模拟器。

The iPhone is actually the simulator, so I don't know if it will run out of memory before the computer does. I know the android emulator doesn't, but I'm not sure about the iOS simulator.

推荐答案

iPhone的输出都显示映射到每一个标准的base64德code PNG格式的开头(如的这个)。为您提供(我修剪,以'/ 9J / 4Q + zRXhpZgAASUkqAAgAAAAKAA4BAgAgAAAAhgAAAA8BAgAFAAAApgAAABABAgAK'是一个有效的base64长度)另一种选择与FFD8的JPEG SOI标记开始,然后包含文本的Exif所以我愿意说是JPEG。

The iPhone output appears to map to the beginning of a PNG per a standard base64 decode (such as this one). The other option you offer (which I trimmed to '/9j/4Q+zRXhpZgAASUkqAAgAAAAKAA4BAgAgAAAAhgAAAA8BAgAFAAAApgAAABABAgAK' to be a valid base64 length) starts with FFD8, the JPEG SOI marker and then contains the text Exif so I'm willing to say is a JPEG.

是否有可能在你不经意间已经连接codeD不同的文件进行比较?

Is it possible you've inadvertently encoded different files for comparison?

呵呵,快尖顺便说一句,如果它可以帮助您测试您的工作 - 在iPhone内置了基地64解码,但没有明显的。关键是要建立一个数据URL这是基地64 EN codeD,并要求在的NSData 加载它。例如。

Oh, quick tip by the way, if it helps you test your work — the iPhone has base 64 decoding built in but not obviously. The trick is to create a data url that's base 64 encoded and ask an NSData to load it. E.g.

NSData *decodedData = [NSData dataWithContentsOfURL:
     [NSURL URLWithString:
         @"data:;base64,/9j/4Q+zRXhpZgAASUkqAAgAAAAKAA4BAgAgAAAAhgAAAA8BAgAFAAAApgAAABABAgAK"]];

数据:; BASE64,直接后跟您的base64数据

Where the data:;base64, is directly followed by your base64 data.

补遗,三年后:由于iOS的7 的NSData 已直接提供 -initWithBase64En codedString:选择:所以没有必要制定一个数据URL了。

Addendum, three years later: since iOS 7 NSData has directly offered -initWithBase64EncodedString:options: so there's no need to formulate a data URL any more.

这篇关于在Base64编码返回iPhone不同的结果比其他工具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆