2016-09-23 36 views
0

在我目前的项目中,我不得不解析base 64字符串,这是由flex(Adobe Flash)应用程序编码和存储。通过与flex开发团队讨论,我发现他们以位图格式存储图像字符串,所以我认为它是一个像素表示。问题隐藏zlib压缩基地64字符串Uiimage

因此,我的第一个问题是,在上述情况下,我可以直接将原始数据转换为使用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; 

}

Here is the output I'm getting , a distorted image :

因此,在图像转换的原始数据的方法任何人都可以提出更好的解决方案离子还是告诉我是否在正确的方向?谢谢,提前:)

仅供参考here你可以找到原来的基地64串

+0

只有方法'+ [UIImage imageWithData:]' –

+0

是啊,我也试过,但是,它给了我零图像对象。在失败之后,我尝试了这个实现。你可以请审查我的实施,并给出任何建议?不管怎么说,还是要谢谢你。 – user3011809

+0

你能否提供原始的base64字符串?因为实际上很难分辨您现在正在使用哪些数据。可能是图像,可能是压缩图像,可能是pdf。 –

回答

1

图像转换为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]; 
} 
+0

我刚刚试过这个,但它不适合我。这是给我零图像对象。谢谢btw。 – user3011809

+0

那么你的字符串可能有些问题..只是帮助你自己:) – vaibhav

+0

实际上这个base 64字符串被压缩了一个,所以我使用一些zlib库扩展来解压缩它。所以应该有与之相关的问题? – user3011809