将zlib压缩的base 64字符串转换为Uiimage的问题

在我当前的一个项目中,我必须解析base 64字符串,该字符串由flex(Adobe Flash)应用程序编码和存储。 通过与flex dev团队讨论,我发现他们以位图格式存储图像字符串,因此我假设它是一个像素表示。

所以,我的第一个问题是,在上面提到的情况下,我可以使用base64解码类直接将原始数据转换为UIimage,还是我必须使用CGBitmapContext?

但是,目前我已经实现了下面提到的转换代码。

`//转换为Base64数据

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];` 

下面是使用核心图形转换图像中的原始数据的方法

 + (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; 

}

这是我得到的输出,一个扭曲的图像:

那么有人可以建议我更好的解决方案或告诉我是否正确的方向吗? 提前致谢 :)

在这里您可以找到原始的基本64字符串

要将图像转换为base64string:

 - (NSString *)imageToNSString:(UIImage *)image { NSData *data = UIImagePNGRepresentation(image); return [data base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed]; } 

要将base64string转换为图像:

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