Storing UIImage in a NSString

Discussion in 'iOS Programming' started by man2manno, Dec 28, 2010.

  1. man2manno macrumors member

    Mar 21, 2009
    Hey guys, is there a way to take a UIImage and store it temporalily as an NSString then decode it later to view elsewhere. I took the image and made it NSData then converted that to an NSString as shown in the code below:

    NSData *imageData = UIImageJPEGRepresentation(imageView.image, 90);
    	NSString *imageString = [[NSString alloc] initWithData:imageData encoding:NSASCIIStringEncoding];
    And ImageString returns a value of weird characters, which I expected, but now when I go to add ImageString to another string using the stringWithFormat method it becomes a null value. Why? Or is there a better way to do this? Thanks.
  2. Svinja, Dec 28, 2010
    Last edited: Dec 28, 2010

    Svinja macrumors newbie

    Oct 27, 2008
    Why do you want to store UIImage as NSString? I am sure this can be avoided.
    Theoritacally you get array of bytes and if you encode it and decode it same way, it should work, i think...
  3. man2manno thread starter macrumors member

    Mar 21, 2009
    Well in the long run, I have an NSString with values that are sent to a php which then places those values into my server. However, I also want it to send an Image along with the values. And the easiest way I can think of doing that would be to encode the image some way into the NSString with the other values and send only one request to my server.
  4. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    You can't do that, at least not reliably. Arbitrary binary data can't be stored as a string, without converting it to an ascii format.

    You might try converting to base64, or some other ascii format.
  5. man2manno thread starter macrumors member

    Mar 21, 2009
    Okay, I have heard about base64, how do you convert an NSData or NSString to base64? Example would be awesome, thanks a lot.

Share This Page