2016-09-23 60 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