2

I'm using web service to retrieve data. But I'm unable to convert data to image. I'm receiving data from server but when I'm converting to image it is showing null.

Dmytro Sadovnychyi
  • 6,171
  • 5
  • 33
  • 60
Balakishore
  • 66
  • 1
  • 1
  • 5

4 Answers4

19

Convert NSData to UIImage :

NSData *data = ... // data that you received
UIImage *image = [UIImage imageWithData:data];

Convert UIImage to NSData (png) :

UIImage *image = [UIImage imageNamed:@"imageName.png"];
NSData *imageData = UIImagePNGRepresentation(image);

Convert UIImage to NSData (jpg) :

UIImage *image = [UIImage imageNamed:@"imageName.jpg"];
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
Jordan Montel
  • 8,227
  • 2
  • 35
  • 40
3
UIImage *image = [UIImage imageWithData:data];
iiFreeman
  • 5,165
  • 2
  • 29
  • 42
1

Check out UIImage *image = [UIImage imageWithData:...];. I'm curious what are you using now?

mmackh
  • 3,550
  • 3
  • 35
  • 51
1

In one of your comments, you suggest that the images "are stored in string". If this is the case, then image may have been base64-encoded (a common mechanism for representing binary data in a string). You should clarify this with the provider of this SOAP service.

If it has been converted to a base64 string, then you'll have to convert that back to the raw binary format before you can use imageWithData or the like. There are a variety of base64 alternatives listed in response to How do I do base64 encoding on iphone-sdk?

Community
  • 1
  • 1
Rob
  • 415,655
  • 72
  • 787
  • 1,044