iOS UIImage生成BMP格式圖片

BMP格式

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

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

NS_ASSUME_NONNULL_END


#import "UIImage+BitmapData.h"


# 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));
        NSLog(@"---%lu", sizeof(rawData));
        if (rawData)
        {
            CGBitmapInfo bitmapInfo = kCGImageByteOrder32Little | 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 * 3) + 54;
    header.reserved1 = 0x0000;
    header.reserved2 = 0x0000;
    header.bitmapOffset = 0x00000036;
    header.headerSize = 0x00000028;
    header.width = width;
    header.height = height;
    header.colorPlanes = 0x0001;
    header.bitsPerPixel = 0x0018;
    header.compression = 0x00000000;
    header.bitmapSize = width * height * 3;
    header.horizontalResolution = 0x00000000;
    header.verticalResolution = 0x00000000;
    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

備注

bitmapFileHeaderData可以跟自己實際需求改寫

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末魄宏,一起剝皮案震驚了整個濱河市当悔,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌绅项,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,470評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件橡疼,死亡現(xiàn)場離奇詭異素邪,居然都是意外死亡,警方通過查閱死者的電腦和手機不狮,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,393評論 3 392
  • 文/潘曉璐 我一進店門降铸,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人摇零,你說我怎么就攤上這事推掸。” “怎么了驻仅?”我有些...
    開封第一講書人閱讀 162,577評論 0 353
  • 文/不壞的土叔 我叫張陵谅畅,是天一觀的道長。 經常有香客問我噪服,道長毡泻,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,176評論 1 292
  • 正文 為了忘掉前任粘优,我火速辦了婚禮仇味,結果婚禮上,老公的妹妹穿的比我還像新娘雹顺。我一直安慰自己丹墨,他們只是感情好,可當我...
    茶點故事閱讀 67,189評論 6 388
  • 文/花漫 我一把揭開白布嬉愧。 她就那樣靜靜地躺著贩挣,像睡著了一般。 火紅的嫁衣襯著肌膚如雪没酣。 梳的紋絲不亂的頭發(fā)上王财,一...
    開封第一講書人閱讀 51,155評論 1 299
  • 那天,我揣著相機與錄音裕便,去河邊找鬼绒净。 笑死,一個胖子當著我的面吹牛闪金,可吹牛的內容都是我干的疯溺。 我是一名探鬼主播论颅,決...
    沈念sama閱讀 40,041評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼哎垦,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了恃疯?” 一聲冷哼從身側響起漏设,我...
    開封第一講書人閱讀 38,903評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎今妄,沒想到半個月后郑口,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體鸳碧,經...
    沈念sama閱讀 45,319評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,539評論 2 332
  • 正文 我和宋清朗相戀三年犬性,在試婚紗的時候發(fā)現(xiàn)自己被綠了瞻离。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,703評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡乒裆,死狀恐怖套利,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情鹤耍,我是刑警寧澤肉迫,帶...
    沈念sama閱讀 35,417評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站稿黄,受9級特大地震影響喊衫,放射性物質發(fā)生泄漏。R本人自食惡果不足惜杆怕,卻給世界環(huán)境...
    茶點故事閱讀 41,013評論 3 325
  • 文/蒙蒙 一族购、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧陵珍,春花似錦联四、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,664評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至伟姐,卻和暖如春收苏,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背愤兵。 一陣腳步聲響...
    開封第一講書人閱讀 32,818評論 1 269
  • 我被黑心中介騙來泰國打工鹿霸, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人秆乳。 一個月前我還...
    沈念sama閱讀 47,711評論 2 368
  • 正文 我出身青樓懦鼠,卻偏偏與公主長得像,于是被迫代替她去往敵國和親屹堰。 傳聞我的和親對象是個殘疾皇子肛冶,可洞房花燭夜當晚...
    茶點故事閱讀 44,601評論 2 353