Проблема в покрытии zlib сжатой строки base 64 в Uiimage

В одном из моих текущих проектов мне нужно проанализировать строку base 64, которая кодируется и сохраняется в приложении flex (Adobe Flash). Обсуждая с командой разработчиков Flex, я обнаружил, что они хранят строку изображения в растровом формате, поэтому я предположил, что это представление в пикселях.

Итак, первый вопрос от меня: в вышеупомянутом случае я могу напрямую преобразовать необработанные данные в UIimage, используя класс декодирования base64, или я должен использовать 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 строки

Ответы на вопрос(1)

Ваш ответ на вопрос