目标C - 的UIImage保存为BMP文件 [英] Objective C - save UIImage as a BMP file

查看:527
本文介绍了目标C - 的UIImage保存为BMP文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似的问题已被要求,但他们总是包含答案用UIImagePNGRe presentation或UIImageJPEGRe presentation,它不会为我工作。我需要将文件保存为位图。

Similar questions have been asked but they always contain the answer "use UIImagePNGRepresentation or UIImageJPEGRepresentation" which will not work for me. I need to save the file as a bitmap.

从我的理解中,UIImage的数据已经是位图。在这种情况下,我不应该创建位图。还是我?我发现有人谁张贴这个问题,但我不能确定确切的答案是什么,因为这是我不清楚。我甚至走得更远,它们甚至创建一个字符指针数组,但我仍然不知道如何将它保存为位图。有没有人在那里谁可以帮助我了解如何将的UIImage保存为位图?或者告诉我的步骤,我会研究的步骤。

From my understanding, the UIImage data is already a bitmap. In which case I shouldn't have to create a bitmap. Or do I? I found someone who posted this question but I'm unsure what exactly the answer is, as it is not clear to me. I've even gone so far as to create a char pointer array but I still don't know how to save it as a bitmap. Is there anybody out there who can help me understand how to save the UIImage as a bitmap? Or tell me the steps and I'll research the steps.

感谢您。

编辑:

我发现本教程(与code)的图像转换为位图,但返回的位图是一个无符号的char *。无论我是在正确的道路上或我要下去了错误的方向。所以现在我要看看我能不能写出来的无符号字符*为.bmp文件。

I found this tutorial (with code) for converting the image to a bitmap but the bitmap returned is an unsigned char *. Either I'm on the right path or I'm going down in the wrong direction. So now I'm going to see if I can write out the unsigned char * to a .bmp file.

推荐答案

下面是一个让我访问原始BGRA数据,以及已经写入到磁盘中的一个合适的位图数据,我为的UIImage拼凑类别头。

Below is the category that I have pieced together for UIImage that lets me access the raw BGRA data as well as data ready to be written to disk with an appropriate bitmap header.

@interface UIImage (BitmapData)
- (NSData *)bitmapData;
- (NSData *)bitmapFileHeaderData;
- (NSData *)bitmapDataWithFileHeader;
@end



# pragma pack(push, 1)
typedef struct s_bitmap_header
{
    // Bitmap file header
    UInt16 fileType;
    UInt32 fileSize;
    UInt16 reserved1;
    UInt16 reserved2;
    UInt32 bitmapOffset;

    // DIB Header
    UInt32 headerSize;
    UInt32 width;
    UInt32 height;
    UInt16 colorPlanes;
    UInt16 bitsPerPixel;
    UInt32 compression;
    UInt32 bitmapSize;
    UInt32 horizontalResolution;
    UInt32 verticalResolution;
    UInt32 colorsUsed;
    UInt32 colorsImportant;
} t_bitmap_header;
#pragma pack(pop)

@implementation UIImage (BitmapData)

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

    size_t bitsPerPixel = 32;
    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 = CGColorSpaceCreateDeviceRGB();

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

        if (rawData)
        {
            CGBitmapInfo bitmapInfo = kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst;
            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 * 4) + 54;
    header.reserved1 = 0x0000;
    header.reserved2 = 0x0000;
    header.bitmapOffset = 0x00000036;
    header.headerSize = 0x00000028;
    header.width = width;
    header.height = height;
    header.colorPlanes = 0x0001;
    header.bitsPerPixel = 0x0020;
    header.compression = 0x00000000;
    header.bitmapSize = height * width * 4;
    header.horizontalResolution = 0x00000B13;
    header.verticalResolution = 0x00000B13;
    header.colorsUsed = 0x00000000;
    header.colorsImportant = 0x00000000;

    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];
}

@end

这篇关于目标C - 的UIImage保存为BMP文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆