Convert UIImage to NSString (and vice versa)

I need a method to convert UIImage to NSString and then convert NSString back to UIImage.

Thank.

+3
source share
3 answers

for> = iOS 7

- (NSString *)imageToNSString:(UIImage *)image
{
    NSData *imageData = UIImagePNGRepresentation(image);

    return [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
}

- (UIImage *)stringToUIImage:(NSString *)string
{
    NSData *data = [[NSData alloc]initWithBase64EncodedString:string
                                                  options:NSDataBase64DecodingIgnoreUnknownCharacters];

    return [UIImage imageWithData:data];
}
+8
source

Instead, convert it to a binary stream ( NSData). It will depend on the format of yours UIImage. If it is JPEG / PNG, for example:

NSData *data1 = UIImageJPEGRepresentation(image, 1.0);
NSData *data2 = UIImagePNGRepresentation(image);

UPDATE: converting binary data to NSStringis a bad idea, so we have a class NSData. The OP wants to be able to send it as a data stream, and then restore it again; NSStringthis will not be required.

+2
source

PNG JPEG, UIImagePNGR UIImageJPEGR, NSData, NSData ( , ). , NSData? / .

0
source

All Articles