0

I was trying to send a UIImage over a simple socket that only accepts CFString for passing data.

I tried converting the UIImage into NSData into NSString into CFString and back, but the final UIImage is always nil.

Conversion:

NSData *dataImage = UIImagePNGRepresentation(image);
NSString *stringImage = [dataImage base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];

Reconversion:

NSString *string = (__bridge NSString *)(object);
NSData *imageData = [[NSData alloc] initWithBase64EncodedString:string options:0];
UIImage *image = [UIImage imageWithData:imageData];
gran_profaci
  • 8,087
  • 15
  • 66
  • 99
  • 1
    From a simpleSocket ? Is it because you didn't retrieve all the data yet (check explaination there: http://stackoverflow.com/questions/36894972/why-doesnt-cast-work-from-nsdata-to-string-swift/36897942#36897942)? Could you try with smaller images, and also, check the value of data/strings? Note that in `NSData *dataImage = [[NSData alloc] init]; dataImage = UIImagePNGRepresentation(image);`, the alloc/init of `dataImage` is useless since you do afterwards `dataImage = UIImagePNGRepresentation(image);`. – Larme Jan 19 '17 at 09:21
  • That makes sense, updated. To put the question in another word - how can I convert a UIImage into something as simple as a CFString and back? – gran_profaci Jan 19 '17 at 11:19
  • On comparing the CFString's sent and received, I can confirm that the entire string is present for conversion. – gran_profaci Jan 19 '17 at 11:20

0 Answers0