Search code examples
iphoneobjective-cipaduiimagensdata

Saving and Loading UIImage from/to string


Using the http://www.ifans.com/forums/showthread.php?t=132024 post from another question i am allowing the user to enter a signature. When the user is finished entering the signature i want to save it away to the database. I want to know if it is possible to save a UIImage to a string at all? And than also reload the UIImage from the string?

Thanks in advance


Solution

  • Yes, you could represent it as NSData and then convert it to NSString:

    NSData *imageData = UIImagePNGRepresentation(yourPNGImage);
    NSString *imageString = [[NSString alloc] initWithBytes: [imageData bytes] length:[imageData length] encoding:NSUTF8StringEncoding];
    

    In case of jpeg images you can easily use UIImageJPEGRepresentation istead of UIImagePNGRepresentation.