Atul
Atul

Reputation: 111

How to get the image from the base64encodedstring ?

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

Upvotes: 0

Views: 185

Answers (1)

Abizern
Abizern

Reputation: 150765

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.

Upvotes: 0

Related Questions