In one my current project, I have to parse base 64 string, which is encoded and stored by flex(Adobe Flash) application. By discussing with flex dev team I found that they storing image string in bitmap format so I assumed it is a pixel representation.
So, first question from me is, in above mentioned case can I directly convert raw data into UIimage using base64 decoding class or I have to use CGBitmapContext ?
However, currently I have implemented below mentioned code for conversion.
` //Convert to Base64 data
NSData *decodedData = [QSStrings decodeBase64WithString:_settingSerializedImage.currentValue];
NSError *error = nil;
NSData *unCompressedData = [decodedData bbs_dataByInflatingWithError:&error];
NSData *dataImage = [NSMutableData new];
NSUInteger bytePosition = 0;
uint8_t * bytes = malloc(5);
[unCompressedData getBytes:&bytes range:NSMakeRange(unCompressedData.length-3, 3)];
bytes = OSSwapBigToHostInt16(bytes);
int width = (int)bytes;
[unCompressedData getBytes:&bytes range:NSMakeRange(unCompressedData.length-5, 5)];
bytes = OSSwapBigToHostInt16(bytes);dataImage = [unCompressedData subdataWithRange:NSMakeRange(0, (unCompressedData.length -5))];
NSUInteger length = [dataImage length];
unsigned char *bytesData = malloc( length * sizeof(unsigned char) ); [dataImage getBytes:bytesData length:length];UIImage *imgScreenShot = [ScreenshotPortlet convertBitmapRGBA8ToUIImage:bytesData withWidth:width withHeight:height];`
below is the method for converting raw data in image using core graphics
+ (UIImage *) convertBitmapRGBA8ToUIImage:(unsigned char *) buffer
withWidth:(int) width
withHeight:(int) height {
char* rgba = (char*)malloc(width*height*4);
for(int i=0; i < width*height; ++i) {
rgba[4*i] = buffer[4*i];
rgba[4*i+1] = buffer[4*i+1];
rgba[4*i+2] = buffer[4*i+2];
rgba[4*i+3] = buffer[4*i+3];
}
//
size_t bufferLength = width * height * 4;
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, rgba, bufferLength, NULL);
size_t bitsPerComponent = 8;
size_t bitsPerPixel = 32;
size_t bytesPerRow = 4 * width;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
if(colorSpaceRef == NULL) {
NSLog(@"Error allocating color space");
CGDataProviderRelease(provider);
return nil;
}
CGBitmapInfo bitmapInfo = kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedFirst;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
CGImageRef iref = CGImageCreate(width,
height,
bitsPerComponent,
bitsPerPixel,
bytesPerRow,
colorSpaceRef,
bitmapInfo,
provider,
NULL,
YES,
renderingIntent);
uint32_t* pixels = (uint32_t*)malloc(bufferLength);
if(pixels == NULL) {
NSLog(@"Error: Memory not allocated for bitmap");
CGDataProviderRelease(provider);
CGColorSpaceRelease(colorSpaceRef);
CGImageRelease(iref);
return nil;
}
CGContextRef context = CGBitmapContextCreate(pixels,
width,
height,
bitsPerComponent,
bytesPerRow,
colorSpaceRef,
bitmapInfo);
if(context == NULL) {
NSLog(@"Error context not created");
free(pixels);
}
UIImage *image = nil;
if(context) {
CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, width, height), iref);
CGImageRef imageRef = CGBitmapContextCreateImage(context);
image = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
CGContextRelease(context);
}
CGColorSpaceRelease(colorSpaceRef);
CGImageRelease(iref);
CGDataProviderRelease(provider);
if(pixels) {
free(pixels);
}
return image;
}
Here is the output I'm getting , a distorted image :
So can anyone suggest me better solution or tell if I'm on right direction in it or not? Thanks, in advance :)
For reference here you can find original base 64 string