Storing UIImage in a NSString

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

  1. man2manno macrumors member

    Joined:
    Mar 21, 2009
    #1
    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:

    Code:
    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

    Joined:
    Oct 27, 2008
    #2
    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

    Joined:
    Mar 21, 2009
    #3
    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 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    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

    Joined:
    Mar 21, 2009
    #5
    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