简体   繁体   中英

How to convert NSData in to UIImage?

I have successfully converted UIImage in to NSData and sending it to a server using SBJson web service. I am using this code:

img=mainImage.image;
NSData *imgdata=UIImagePNGRepresentation(img);

NSString *post =[[NSString alloc] initWithFormat:@"gid=%@&image=%@",[lblgid text],imgdata];

NSURL *url=[NSURL URLWithString:@"http://www.abchjahdkahdakjskahdk/updategameimage.php"];

here img is UIImage, mainImage is an UIImageView. Everything is going good from this end. Now on other end where i have to get the Image there ia m facing problem. I am using this code:

 NSData *imgdata=[abc valueForKey:@"image"];

    imge = [UIImage imageWithData:imgdata];

    imgview=[[UIImageView alloc]initWithImage:imge];

When i print imgdata it is returning the same data as i send from other end but when i am trying to convert this data back in to an UIImage (in second line of above code) it is throwing an exception.

2013-01-22 14:23:43.298 Drase[946:11303] -[__NSArrayI length]: unrecognized selector sent to instance 0x7566800 2013-01-22 14:23:43.298 Drase[946:11303] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI length]: unrecognized selector sent to instance 0x7566800' * First throw call stack: (0x1ce1012 0x111ee7e 0x1d6c4bd 0x1cd0bbc 0x1cd094e 0x1cb1b2 0x881f3 0x87ef4 0x1c659 0x1132705 0x69920 0x698b8 0x12a671 0x12abcf 0x129d38 0x9933f 0x99552 0x773aa 0x68cf8 0x1c3cdf9 0x1c3cad0 0x1c56bf5 0x1c56962 0x1c87bb6 0x1c86f44 0x1c86e1b 0x1c3b7e3 0x1c3b668 0x6665c 0x29d2 0x2905) libc++abi.dylib: terminate called throwing an exception.

I am not getting any solution for this problem please help me out.

i think abc is NSArray type ie immutable, when you execute this line

imge = [UIImage imageWithData:imgdata];

it has referenced with abc NSArray because of imgdata

here you are indirectly trying to modify it, thats why it is calling to [__NSArrayI length] . but in real there is no such method , because we can count the NSArray but cant measure Length.that means there is no such method thats why it is giving error

[__NSArrayI length]: unrecognized selector sent to instance

Updated 1:

try to do something like this:

NSData *imgdata=[[abc valueForKey:@"image"]objectAtIndex:0];

try

  NSData *data = yourData;

  UIImage *image = [UIImage imageWithData:data];

or

  NSData *data = yourData;

  UIImage *image = [[UIImage alloc] initWithData:data];
  ....
  [image release];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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