简体   繁体   English

NSData/UIImage 转字符串

[英]NSData/UIImage to String

Is it possible to convert NSData/UIImage Data Representation as JPEG to a String, to be sent over HTTP to a PHP File to save this string in a database, and then retrive it later on in the application and convert it back into an NSData/UIImage Object?是否可以将 NSData/UIImage 数据表示作为 JPEG 转换为字符串,通过 HTTP 发送到 PHP 文件以将此字符串保存在数据库中,然后稍后在应用程序中检索它并将其转换回 NSData/ UIImage 对象?

I have tried Base64 Encoding Libraries but base64 doesn't seem valid as the image doesn't display correctly on a HTML Page.我尝试过 Base64 编码库,但 base64 似乎无效,因为图像在 HTML 页面上无法正确显示。

Any suggestions?有什么建议么?

Edit.编辑。

I was using the following library:我正在使用以下库:

http://www.imthi.com/blog/programming/iphone-sdk-base64-encode-decode.php http://www.imthi.com/blog/programming/iphone-sdk-base64-encode-decode.php

And converting in the following way:并按以下方式转换:

 NSData *imageData = UIImageJPEGRepresentation(MyImage.image, 90);
[Base64 initialize];
NSData *encoded = [Base64 encode:imageData];
NSLog(@"%@",encoded);

This does chug out alot of BASE64 but when I save it to a file and try to view it, I just get the eror loading image [?] in Chrome.这确实产生了很多 BASE64,但是当我将它保存到一个文件并尝试查看它时,我只是在 Chrome 中得到错误加载图像 [?]。

Thanks谢谢

The point of encoding an NSData object to base 64 is so you can represent the data as a string that can be stored or transferred more easily.NSData对象编码为 base 64 的要点是,您可以将数据表示为可以更轻松地存储或传输的字符串。 You then need to decode the base 64 encoded string back into NSData .然后,您需要将 base 64 编码的字符串解码回NSData This data can then be used to create a new UIImage .然后可以使用这些数据来创建一个新的UIImage Your server needs to do this decoding to get back the original data.您的服务器需要进行此解码才能取回原始数据。

Your code has a mistake.你的代码有错误。 This line:这一行:

NSData *encoded = [Base64 encode:imageData];

should be:应该:

NSString *encoded = [Base64 encode:imageData];

Notice that you get back a string, not data.请注意,您返回的是字符串,而不是数据。

You commented that you wrote the encoded string to a file then couldn't view the image.您评论说您将编码的字符串写入文件,然后无法查看图像。 Of course not.当然不是。 If you want to write the image data to a file so the file is actually viewable as the image, then don't encode the data first.如果要将图像数据写入文件,以便文件实际上可以作为图像查看,则不要先对数据进行编码。 Write the raw image data to a file.将原始图像数据写入文件。

you can convert image to string like this first convert your UIImage to NSData & then convert that ata into string by using encodeBase64WithData您可以像这样将图像转换为字符串,首先将您的 UIImage 转换为 NSData,然后使用 encodeBase64WithData 将该 ata 转换为字符串

NSString *imageOne = [self encodeBase64WithData:[imageDict objectForKey:@"ImageOne"]];

and again string to UIImage like this:并再次字符串到 UIImage 像这样:

[UIImage imageWithData: [self decodeBase64WithString:[registerDataDict objectForKey:@"imageOne"]]];

You need to import Base64.h你需要导入Base64.h

You can directly use like this way:您可以像这样直接使用:

 UIImage *image = _btnaddCountryIcon.imageView.image;
 NSData *imageData = UIImagePNGRepresentation(image);
 NSString *base64 = [Base64 encode:imageData];

directly you can convert to NSString.可以直接转换为NSString。 This code works fine for me.这段代码对我来说很好用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM