将UIImage保存为8位灰度位图

时间:2015-05-22 17:49:12

标签: ios objective-c cocoa-touch bitmap core-graphics

我正在尝试将UIImage输出到8位灰度位图,但我对图像处理并不是很熟悉,所以我对如何做到这一点感到茫然。我按照这个stackoverflow文章(Objective C - save UIImage as a BMP file)来创建一个UIImage类别,它可以成功生成一个RGB 32位图像,但我无法操纵代码使其以8位灰度输出。我试图获取此图像的原始字节,以便传递给提供的C库。

我尝试为哪个UIImage生成位图是否重要?可以是每个像素的任意位数,每个组件的位数和alpha设置吗?我尝试在没有alpha的8位UIImage和使用alpha的32位上使用此代码,并且两者都没有创建可由OS X打开的位图。

我希望有人可以提供帮助!我已经敲了几天头!

- (NSData *)bitmapData
{
    NSData          *bitmapData = nil;
    CGImageRef      image = self.CGImage;
    CGContextRef    context = NULL;
    CGColorSpaceRef colorSpace;
    UInt8           *rawData;

    size_t bitsPerPixel = 8;
    size_t bitsPerComponent = 8;
    size_t bytesPerPixel = bitsPerPixel / bitsPerComponent;

    size_t width = CGImageGetWidth(image);
    size_t height = CGImageGetHeight(image);

    size_t bytesPerRow = width * bytesPerPixel;
    size_t bufferLength = bytesPerRow * height;

    colorSpace = CGColorSpaceCreateDeviceGray();

    if (colorSpace)
    {
        // Allocate memory for raw image data
        rawData = (UInt8 *)calloc(bufferLength, sizeof(UInt8));

        if (rawData)
        {
            CGBitmapInfo bitmapInfo = (CGBitmapInfo)kCGImageAlphaNone;
            context = CGBitmapContextCreate(rawData,
                                            width,
                                            height,
                                            bitsPerComponent,
                                            bytesPerRow,
                                            colorSpace,
                                            bitmapInfo);

            if (context)
            {
                CGRect rect = CGRectMake(0, 0, width, height);

                CGContextTranslateCTM(context, 0, height);
                CGContextScaleCTM(context, 1.0, -1.0);
                CGContextDrawImage(context, rect, image);

                bitmapData = [NSData dataWithBytes:rawData length:bufferLength];

                CGContextRelease(context);
            }

            free(rawData);
        }

        CGColorSpaceRelease(colorSpace);
    }

    return bitmapData;
}

- (NSData *)bitmapFileHeaderData
{
    CGImageRef image = self.CGImage;
    UInt32     width = (UInt32)CGImageGetWidth(image);
    UInt32     height = (UInt32)CGImageGetHeight(image);

    t_bitmap_header header;

    header.fileType = 0x4D42;
    header.fileSize = (height * width) + 54;
    header.reserved1 = 0;
    header.reserved2 = 0;
    header.bitmapOffset = 54;
    header.headerSize = 40;
    header.width = width;
    header.height = height;
    header.colorPlanes = 1;
    header.bitsPerPixel = 8;
    header.compression = 0;
    header.bitmapSize = height * width;
    header.horizontalResolution = 0;
    header.verticalResolution = 0;
    header.colorsUsed = 0;
    header.colorsImportant = 0;

    return [NSData dataWithBytes:&header length:sizeof(t_bitmap_header)];
}

- (NSData *)bitmapDataWithFileHeader
{
    NSMutableData *data = [NSMutableData dataWithData:[self bitmapFileHeaderData]];
    [data appendData:[self bitmapData]];

    return [NSData dataWithData:data];
}

1 个答案:

答案 0 :(得分:0)

如果您的UIImage没有Alpha通道,可以将其转换为灰度,如下所示:

- (UIImage *)grayscaleImage:(UIImage *)image {
    CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
    CGContextRef context = CGBitmapContextCreate(nil, image.size.width, image.size.height, 8, 0, colorSpace, (CGBitmapInfo)kCGImageAlphaNone);
    CGContextDrawImage(context, imageRect, [image CGImage]);
    CGImageRef imageRef = CGBitmapContextCreateImage(context);
    UIImage *grayscaleImage = [UIImage imageWithCGImage:imageRef];
    CGColorSpaceRelease(colorSpace);
    CGContextRelease(context);
    CFRelease(imageRef);
    return grayscaleImage;
}