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;
}