gpt4 book ai didi

ios - 将 zlib 压缩的 base64 字符串转换为 Uiimage 时出现问题

转载 作者:行者123 更新时间:2023-11-29 00:40:25 39 4
gpt4 key购买 nike

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

所以,我的第一个问题是,在上述情况下,我可以使用 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];`

下面是使用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 :

那么谁能建议我更好的解决方案,或者告诉我我的方向是否正确?谢谢,提前:)

供引用here你可以找到原始的 base 64 字符串

最佳答案

将图片转换成base64字符串:

- (NSString *)imageToNSString:(UIImage *)image
{
NSData *data = UIImagePNGRepresentation(image);

return [data base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
}

将base64字符串转换为图片:

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

return [UIImage imageWithData:data];
}

关于ios - 将 zlib 压缩的 base64 字符串转换为 Uiimage 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39657434/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com