以字符串格式保存图像(在 xml 中使用)..不起作用
我正在将图像从选择器转换为 nsdata(jpeg 表示),然后使用以下代码将其转换为 nsstring
NSData *data=UIImageJPEGRepresentation(image,1.0);
NSString *imageString=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
[[NSUserDefaults standardUserDefaults] setObject:imageString forKey:@"image_name"];
,在另一端我需要显示图像的 uiimage 形成如下。
NSString *imageString=[[NSString alloc] init];
imageString=[[NSUserDefaults standardUserDefaults] objectForKey:@"image_name"];
UIImage *image=[UIImage imageWithData:[imageString dataUsingEncoding:NSUTF8StringEncoding]];
顶部代码中使用的图像变量不是 nil ,但是由数据形成的图像变得为零...当我记录 userdefaults 时,上面提到的键存在一些字符串。任何人都可以解释为什么会这样..这是什么正确的方法来做到这一点
i am converting image from picker into nsdata(jpeg representation) and then converting it into nsstring using the following code
NSData *data=UIImageJPEGRepresentation(image,1.0);
NSString *imageString=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
[[NSUserDefaults standardUserDefaults] setObject:imageString forKey:@"image_name"];
and at the other end where i need to display the image the uiimage is formed as follows.
NSString *imageString=[[NSString alloc] init];
imageString=[[NSUserDefaults standardUserDefaults] objectForKey:@"image_name"];
UIImage *image=[UIImage imageWithData:[imageString dataUsingEncoding:NSUTF8StringEncoding]];
the image variable used in the top code is not nil ,but the image formed from data is getting nil...when i nslogged userdefaults some string is present for the key mentioned above .can anyone explain why is this so..what is the right way to do this
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果它通过 Web 服务器等,您可以使用 Base64 编码/解码或其他一些普通编码器对其进行封装。
它删除“坏”字符,即在转换过程中弄乱字符串的字符,并将它们更改为通用字母字符,然后再返回。
如果这是您问题的原因,这是我使用的一个简短的(我很可能是偷来并改编的,但不记得从谁那里。抱歉!:-))
base64helper.h
base64helper.m
If it goes through a web server or the like, you could encapsulate it with base64 enc/decoding or some other plain encoder.
It removes "bad" char, ie that screw up the string during transformation, and change them to generic alphabetical chars and then back again.
if this is the reason to your issues, here is a short one i use (which I most probably stole and adapted, but do not remember from whom. Sorry! :-) )
base64helper.h
base64helper.m