在iOS中处理各种类型的位图

问题描述 投票:1回答:2

ios-converting-uiimage-to-rgba8-bitmaps-and-back是一篇非常好的文章,我发现它描述了我们如何处理位图缓冲区和UIImages。本文介绍RGBA32 / RGBA8位图图像。使用大小为宽*高* 4的char *缓冲区创建位图图像。即,每个像素信息将具有4个字节的数据,每个1字节用于分别存储红色,绿色,蓝色和alpha。在创建位图图像时,给出位图信息'kCGImageAlphaPremultipliedLast'。 CGColorSpaceCreateDeviceRGB()用于将bitmapbuffer转换回UIImage。通过更改位图信息,我们还可以处理RGBA 24图像。我需要处理RGBA 5551位图图像。红色,绿色和蓝色给出5位表示分辨色,1位用于存储alpha值。如果我们要创建这样的位图,我们如何为char *位图分配缓冲区。是否可以转换为UIImage数据类型?任何帮助将不胜感激。

ios objective-c bitmap uiimage
2个回答
0
投票

BITS_PER_COMPONENT为5,BITS_PER_COMPONENT为16.使用此代码,我已成功完成。 kCGBitmapByteOrder16Little表示char缓冲区的字节顺序。

size_t bufferLength = width * height * 2;
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer, bufferLength, NULL);
size_t bitsPerComponent = BITS_PER_COMPONENT;
size_t bitsPerPixel = BITS_PER_PIXEL;
size_t bytesPerRow = 2 * width;

CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
if(colorSpaceRef == NULL) {
    NSLog(@"Error allocating color space");
    CGDataProviderRelease(provider);
    return nil;
}
CGBitmapInfo bitmapInfo = kCGBitmapByteOrder16Little | kCGImageAlphaNoneSkipFirst;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

CGImageRef iref = CGImageCreate(width,
                                height,
                                bitsPerComponent,
                                bitsPerPixel,
                                bytesPerRow,
                                colorSpaceRef,
                                bitmapInfo,
                                provider,   // data provider
                                NULL,       // decode
                                YES,            // should interpolate
                                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);

0
投票

你需要创建16位图像,所以提供bpp为16,bpc为5,下面将是代码:

size_t width = CGImageGetWidth(screenShotImageRef);
size_t height = CGImageGetHeight(screenShotImageRef);
size_t bytesPerRow = width * (bpc == 5 ? 2 : 4);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef bitmapContext = CGBitmapContextCreate(buffer(provide buffer where you want to write image into), width, height, bpc, bytesPerRow, colorSpace, kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrderDefault);
CGContextDrawImage(bitmapContext, CGRectMake(0, 0, width, height), screenShotImageRef);
CGContextRelease(bitmapContext);
CGColorSpaceRelease(colorSpace);
© www.soinside.com 2019 - 2024. All rights reserved.