如何从base64encodedstring获取图像?(How to get the image from the base64encodedstring ?)

编程入门 行业动态 更新时间:2024-10-22 12:32:04
如何从base64encodedstring获取图像?(How to get the image from the base64encodedstring ?)

我正在使用base64encoding将UIImage发送到服务器,然后在另一端我收回它,将base64encodedstring转换为NSData然后尝试将我的图像恢复到UIImageView上。

一切正常工作发送Base64encodedString并接收它,但当我将NSData转换回UIImage时,它抛出以下异常

- [__ NSArrayI dataUsingEncoding:allowLossyConversion:]异常与Base64编码一起提交

这是我用于发布图像的代码:

img=mainImage.image; NSData *imgdata=UIImagePNGRepresentation(img); NSString *imgstring=[imgdata base64EncodedString]; NSString *post =[[NSString alloc] initWithFormat:@"gid=%@&image=%@",[lblgid text],imgstring]; NSLog(@"PostData: %@",post); NSURL *url=[NSURL URLWithString:@"http://www.abcd/updategameimage.php"];

以下是接收端的代码

NSString *imgStr=[abc valueForKey:@"image"]; NSLog(@"%@",imgStr); NSData *imgdata=[NSData dataWithBase64EncodedString:imgStr]; imge = [UIImage imageWithData:imgdata]; [imgview setImage:imge];

这里abc是NSMutableArray

I am using base64encoding for sending an UIImage to the server and then on the other end i am getting it back, converting the base64encodedstring in to NSData then trying to get my image back on an UIImageView.

Everything working fine sending the Base64encodedString and receiving it but when i am converting the NSData back in to UIImage it is throwing the following exception

-[__NSArrayI dataUsingEncoding:allowLossyConversion:] exception is comming with Base64 encoding

this is the code i am using for posting the image:

img=mainImage.image; NSData *imgdata=UIImagePNGRepresentation(img); NSString *imgstring=[imgdata base64EncodedString]; NSString *post =[[NSString alloc] initWithFormat:@"gid=%@&image=%@",[lblgid text],imgstring]; NSLog(@"PostData: %@",post); NSURL *url=[NSURL URLWithString:@"http://www.abcd/updategameimage.php"];

And following is the code at receiving end

NSString *imgStr=[abc valueForKey:@"image"]; NSLog(@"%@",imgStr); NSData *imgdata=[NSData dataWithBase64EncodedString:imgStr]; imge = [UIImage imageWithData:imgdata]; [imgview setImage:imge];

Here abc is NSMutableArray

最满意答案

你确定abc是一个mutableArray吗?

因为您正在向它发送一个valueForKey:方法。 这是由NSObject实现的KVC方法,将此消息发送给它是没有意义的。

如果您打算将objectForKey:方法发送给它 - 那么这将是您发送到字典而不是数组的消息。

而且,你得到的错误:

- [__ NSArrayI dataUsingEncoding:allowLossyConversion:]异常与Base64编码一起提交

告诉你NSArray没有响应dataUsingEncoding:allowsLossyConversion: messages - 这是一个NSString对象的方法。

我认为你对你的对象类型感到困惑,需要更好地处理什么是什么。

Are you sure abc is a mutableArray?

Because you are sending it a valueForKey: method. This is a KVC method implemented by NSObject and it doesn't make sense to send it this message.

If you meant to send it the objectForKey: method instead - then that would be a message that you send to dictionaries, not arrays.

And, the error that you are getting:

-[__NSArrayI dataUsingEncoding:allowLossyConversion:] exception is comming with Base64 encoding

is telling you that an NSArray does not respond to dataUsingEncoding:allowsLossyConversion: messages - which is a method for NSString objects.

I think you are getting confused with your object types and need to get a better handle on what is what.

更多推荐

本文发布于:2023-07-16 07:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1125469.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   base64encodedstring   image

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!