-1

I have a jpg image which I am converting to data using the UIImageJPEGRepresentation function

I am then sending the data to a server online and getting it back when it is needed though I get this string:

<ffd8ffe0 00104a46 49460001 01000001 00010000 ffe10058 45786966 00004d4d...>

And I don't know how to convert it back to an image

Thanks in advance

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Hive7
  • 3,599
  • 2
  • 22
  • 38

4 Answers4

1

I managed to find a way to do it by converting the string to a base64 string like this:

+ (NSString *) encodeToBase64:(NSData *) rawBytes {
    return [StringMethods encodeToBase64:(const uint8_t*) rawBytes.bytes length:rawBytes.length];
}

+ (NSString *) encodeToBase64:(const uint8_t *)input length:(NSInteger) length {
    NSMutableData *data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
    uint8_t *output = (uint8_t *)data.mutableBytes;
    for (NSInteger i = 0; i < length; i += 3) {
        NSInteger value = 0;
        for (NSInteger j = i; j < (i + 3); j++) {
            value <<= 8;
            if (j < length) {
                value |= (0xFF & input[j]);
            }
        }

        NSInteger index = (i / 3) * 4;
        output[index + 0] =                    encodingTable[(value >> 18) & 0x3F];
        output[index + 1] =                    encodingTable[(value >> 12) & 0x3F];
        output[index + 2] = (i + 1) < length ? encodingTable[(value >> 6)  & 0x3F] : '=';
        output[index + 3] = (i + 2) < length ? encodingTable[(value >> 0)  & 0x3F] : '=';
    }
    return [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
}

Then I saved it onto the server then when loading back in I used this function:

- (NSData *)base64DataFromString: (NSString *)string {
    unsigned long ixtext, lentext;
    unsigned char ch, inbuf[4], outbuf[3];
    short i, ixinbuf;
    Boolean flignore, flendtext = false;
    const unsigned char *tempcstring;
    NSMutableData *theData;
    if (string == nil){
        return [NSData data];
    }
    ixtext = 0;
    tempcstring = (const unsigned char *)[string UTF8String];
    lentext = [string length];
    theData = [NSMutableData dataWithCapacity: lentext];
    ixinbuf = 0;
    while (true) {
        if (ixtext >= lentext) {
            break;
        }
        ch = tempcstring [ixtext++];
        flignore = false;
        if ((ch >= 'A') && (ch <= 'Z')) {
            ch = ch - 'A';
        } else if ((ch >= 'a') && (ch <= 'z')) {
            ch = ch - 'a' + 26;
        } else if ((ch >= '0') && (ch <= '9')) {
            ch = ch - '0' + 52;
        } else if (ch == '+') {
            ch = 62;
        } else if (ch == '=') {
            flendtext = true;
        } else if (ch == '/') {
            ch = 63;
        } else {
            flignore = true;
        }
        if (!flignore) {
            short ctcharsinbuf = 3;
            Boolean flbreak = false;
            if (flendtext) {
                if (ixinbuf == 0) {
                    break;
                }
                if ((ixinbuf == 1) || (ixinbuf == 2)) {
                    ctcharsinbuf = 1;
                } else {
                    ctcharsinbuf = 2;
                }
                ixinbuf = 3;
                flbreak = true;
            }
            inbuf [ixinbuf++] = ch;
            if (ixinbuf == 4) {
                ixinbuf = 0;
                outbuf[0] = (inbuf[0] << 2) | ((inbuf[1] & 0x30) >> 4);
                outbuf[1] = ((inbuf[1] & 0x0F) << 4) | ((inbuf[2] & 0x3C) >> 2);
                outbuf[2] = ((inbuf[2] & 0x03) << 6) | (inbuf[3] & 0x3F);
                for (i = 0; i < ctcharsinbuf; i++) {
                    [theData appendBytes: &outbuf[i] length: 1];
                }
            }
            if (flbreak) {
                break;
            }
        }
    }
    return theData;
}

And finally to convert it into an image I was this simple method:

UIImage *image = [UIImage imageWithData:imageData];
Hive7
  • 3,599
  • 2
  • 22
  • 38
0

Is what you are showing a string, or is that what you get when you display NSData? I believe that if you log binary data, it comes out as you've shown.

What you have shown is hexadecimal data, not base64, as the other poster suggested.

You could either write code that uses sscanf() to pull out int values from your hex string, or create an NSScanner object and use it's scanHexInt method. If you google "convert hex NSString to integer" you should find both approaches.

Since your values have spaces between them, NSScanner might be your best bet.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • I send what I said above to the database and then when I get it back I get it as it is above though in the form of a string – Hive7 Jun 08 '14 at 13:55
  • If you're putting the data in a database you should write/read it as a "blob", starting/ending with NSData objects. Then the data will not get corrupted in the transitions. – Hot Licks Jun 08 '14 at 14:24
  • (If you don't use a blob you must convert the data to Base64 and back.) – Hot Licks Jun 08 '14 at 14:25
  • Hot Licks, that's nonsense. You can transmit data via Morse code if you want to. Hex encoding isn't as efficient as Base64, but it works. As mentioned in my post, NSScanner will peel off integers from the data, which you would then save "byte wise" into your NSData. – Duncan C Jun 08 '14 at 14:31
  • I'm not talking about transmission, I'm talking about inserting the data into a database. – Hot Licks Jun 08 '14 at 14:37
0

Issue

You are sending NSData to server not NSString, and that's why you are getting this NSData on that format read by you as NSString.

Solution

First you need to send it correctly as NSString to server by doing this:

NSData *imageData= UIImagePNGRepresentation(yourImage);
NSString *encodedString = [imageData base64EncodedStringWithOptions:0];

After you send it to NSString, now you get it back an manipulate it like this. I am using NSData Base64 library to perform this task.

You just manually copy these files to your project. Import the classes to your project and these lines to your code:

#import "NSData+Base64.h"

//This function converts your base64string (NSString Class) to NSData
NSData *data = [[NSData alloc] initWithData:[NSData dataFromBase64String:base64String]];

//Here is where you convert it to UIImage
UIImage *image = [UIImage imageWithData:data];

P.S You can also follow your answer, which to me seems correct, but I just wanted to let you know where were you doing wrong.

Community
  • 1
  • 1
E-Riddie
  • 14,660
  • 7
  • 52
  • 74
-1

This seems to be a good answer for converting:

NSData *dataImage = [[NSData alloc] init];
dataImage = UIImagePNGRepresentation(image);
NSString *stringImage = [dataImage base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];

which you can find here:NSString to UIImage

Community
  • 1
  • 1
user2277872
  • 2,963
  • 1
  • 21
  • 22
  • Sorry I might have misworded it I can get the image data to and from the web server though I can't now convert the data, which is in the form of a string, to an image – Hive7 Jun 08 '14 at 13:42
  • Actually that won't work as I can't convert the string to data because it changes @AlessandroDiaferia – Hive7 Jun 08 '14 at 13:46