Saving and loading UIImage from / to a string

Using the message http://www.ifans.com/forums/showthread.php?t=132024 from another question, I allow the user to enter a signature. When the user finishes entering the signature, I want to save it to the database. I want to know if it is possible to save UIImage to a string at all? And how to reload UIImage from a string?

Thanks in advance

+3
source share
3 answers

Yes, you could introduce 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 the case of jpeg images, you can easily use UIImageJPEGRepresentationistead UIImagePNGRepresentation.

+3
source

UIImage Base64:

, .

  • NSData Base64.

  • [UIImage initWithData] UIImage.

+4
NSData *data = UIImageJPEGRepresentation(Imagename, 1.0f);
NSString *imageString = [NSString stringWithFormat:@"%@",data];
+2
source

Source: https://habr.com/ru/post/1795163/


All Articles