0

Maybe I need to scale down my question to something a little more simple.

How do I get the byte string into a CGImageRef ?

Here, I am using a CFDataRef and a CGDataProviderRef. Maybe there is a better way?


I am receiving a base64 encoded image as a std::string. Now I need to stuff it back into a CGImageRef to get the data back into the correct format to populate a UIImage. If the data is not in the correct format the UIImage will be nil.

I've been looking for how to do this for days now and I cannot seem to figure this out. Here is my code for trying to convert the image:

- (UIImage *)testProcessedImage:(std::string)byteString
{
    CFDataRef dataRef = CFDataCreate( NULL, (const UInt8*) byteString.data(), byteString.length() );
    CGDataProviderRef dataProvider = CGDataProviderCreateWithCFData(dataRef);

    // Code below does not appear to work
    CGImageRef imageRef = CGImageCreateWith ....... ;  // Maybe ?
    UIImage *image = [UIImage imageWithCGImage:imageRef];
    UIImage *image3 = [UIImage imageWithCGImage:(__bridge CGImageRef)(image)];
    return image3;
}

There is a lot of code out there that has helped me get to this point but I'm stuck. The above code returns nil and I cannot figure out how to get it right.


If anybody wants to test this, here is the code that takes a UIImage and simulates the server-side code by converting the image into a std::string. Thanks in advance for your help.


- (UIImage *)testChangeImageToBase64String
{
    UIImage *processedImage = [UIImage imageNamed:@"myFile.jpg"];

    // UIImage to unsigned char *
    CGImageRef imageRef = processedImage.CGImage;
    NSData *data1 = (NSData *) CFBridgingRelease(CGDataProviderCopyData(CGImageGetDataProvider(imageRef)));

    //UIImage *image = [UIImage imageWithCGImage:imageRef];

    unsigned char *pixels = (unsigned char *)[data1 bytes];
    unsigned long size = [data1 length];

    // ***************************************************************************
    // This is where we would call transmit and receive the bytes in a std::string
    // ***************************************************************************

    // unsigned char * to string
    const std::string byteString(pixels, pixels + size);

    UIImage *newImage = [self testProcessedImage:byteString];
    return newImage;
}
Leigh
  • 28,765
  • 10
  • 55
  • 103
Patricia
  • 5,019
  • 14
  • 72
  • 152
  • I think the main issue with this is you're expecting a constant data representation from UIImage. The framework makes no such guarantee. Perhaps a more direct route (`+[NSData dataWithContentsOfFile:]`) would be a better idea. – CodaFi Jul 22 '14 at 00:03
  • The closest information to what I am trying to do is in this question (http://stackoverflow.com/q/2261177/1735836), except I am not dealing with a byte array. I have a byte string. I'm not sure how to use dataWithContentsOfFile for this purpose. Here's another question that is similar (http://stackoverflow.com/q/3764512/1735836). Thank you. – Patricia Jul 22 '14 at 00:13
  • Could this be the answer? http://stackoverflow.com/a/24899398/1735836 – Patricia Jul 23 '14 at 00:13

0 Answers0