如何获取图像的有效base64字符串 [英] How to get valid base64 string for an image

查看:174
本文介绍了如何获取图像的有效base64字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码获取我的图像的base64编码字符串:

I use the following code to get a base64 encoded string of my image:

UIImage *image = [photoView image];
NSString *encoded = [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];

图像是800 * 800像素,但生成的字符串太大,以至于当我的浏览器崩溃时我将其粘贴到文本框中。我将它粘贴在Dreamweaver中以计算线条,它超过300.000行。这可能不对。如果我将相同的图片上传到此网站,则会生成base64字符串约200行。

The image is 800*800 pixels, but the generated string is so big that it crashes my browser when I paste it in a textbox. I pasted it in dreamweaver to count the lines and it is over 300.000 lines. This can't be right. If I upload the same picture to this site it generates a base64 string of about 200 lines.

如何在目标c中生成正常的base64字符串?我实际可以使用的一个......

How do I generate a 'normal' base64 string in objective c? One that I can actually use...

正如Martin R所建议我检查了UIImagePNGRepresentation(image)返回的数据长度,它是4502370字节。这可能是问题的一部分。可能是在UIImageView中绘制图像并稍后检索它就像我正在做的那样以屏幕尺寸返回图像吗?

As Martin R suggested I checked the data length of what UIImagePNGRepresentation(image) returns and it is 4502370 bytes. This is probably a part of the problem. Could it be that drawing an image in a UIImageView and retrieving it later like I'm doing gives back an image at the screen size?

推荐答案

通过使用 NSDataBase64Encoding64CharacterLineLength ,您将每行限制为64个字符(如文档中所述)。尝试:

By using NSDataBase64Encoding64CharacterLineLength you limit each line to 64 characters (as is written in the documentation). Try:

NSString *encoded = [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:0];

这篇关于如何获取图像的有效base64字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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