ios用objective-c进行jpg图片转bmp,图片有问题。
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "UIImage+BitmapData.h"
int main(int argc, char *argv[]) {
@autoreleasepool {
//图片转base64
UIImage *image = [UIImage imageNamed:@"11.png"];
UIImage *ub = [[UIImage alloc] init];
[ub bitmapDataWithFileHeader:image];
}
return 0;
}
#import "UIImage+BitmapData.h"
# pragma pack(push, 1)
typedef struct s_bitmap_header
{
// Bitmap file header
UInt16 fileType;//文件类型 规定为'BM'
UInt32 fileSize;//BMP图像文件的大小
UInt16 reserved1;//总为0
UInt16 reserved2;//总为0
UInt32 bitmapOffset;//BMP图像数据的地址
// DIB Header
UInt32 headerSize;// infoHeader这个头文件的大小
UInt32 width;// 图片宽多少像素
UInt32 height;//高多少
UInt16 colorPlanes;//默认1
UInt16 bitsPerPixel;
UInt32 compression;
//压缩方式,0表示不压缩,1表示RLE8压缩,2表示RLE4压缩,3表示每个像素值由指定的掩码决定
UInt32 bitmapSize;//图片大小
UInt32 horizontalResolution;//水平分辨率,单位像素/m
UInt32 verticalResolution;//垂直分辨率,单位像素/m
UInt32 colorsUsed;//BMP图像使用的颜色,0表示使用全部颜色
UInt32 colorsImportant;//重要的颜色数,此值为0时所有颜色都重要
} t_bitmap_header;
#pragma pack(pop)
@implementation UIImage (BitmapData)
- (NSData *) bitmapData :(UIImage *) img
{
NSData *bitmapData = nil;
CGImageRef image = img.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 = 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 :(UIImage *) img
{
CGImageRef image = img.CGImage;
UInt32 width = (UInt32)CGImageGetWidth(image);
UInt32 height = (UInt32)CGImageGetHeight(image);
t_bitmap_header header;
header.fileType = 0x4D42;//0x4D42
//header.fileSize = (height * width * 3) + 54;
header.fileSize = (height * width * 3) + 54;
header.reserved1 = 0x0000;//0
header.reserved2 = 0x0000;//0
header.bitmapOffset = 0x00000036;
//从文件开始到位图数据开始之间的数据(bitmap data)之间的偏移量
header.headerSize = 0x00000028;// infoHeader这个头文件的大小
header.width = width;//图像宽度,单位为像素
header.height = height;//图像高度,单位为像素
header.colorPlanes = 0x0001;//默认1
header.bitsPerPixel = 0x0018;//每个像素比特数
header.compression = 0x00000000;
//压缩方式,0表示不压缩,1表示RLE8压缩,2表示RLE4压缩,3表示每个像素值由指定的掩码决定
header.bitmapSize = width * height * 3;//图片大小
header.horizontalResolution = 0x00000B13;//水平分辨率,单位像素/m
header.verticalResolution = 0x00000B13;//垂直分辨率,单位像素/m
header.colorsUsed = 0x00000000;//BMP图像使用的颜色,0表示使用全部颜色
header.colorsImportant = 0x00000000;//重要的颜色数,此值为0时所有颜色都重要
return [NSData dataWithBytes:&header length:sizeof(t_bitmap_header)];
}
- (NSData *)bitmapDataWithFileHeader :(UIImage *) img
{
NSMutableData *data = [NSMutableData dataWithData:[self bitmapFileHeaderData: img]];
[data appendData:[self bitmapData:img]];
int value = arc4random() % 1000;
NSString* fileName = [NSString stringWithFormat:@"%d%@",value,@".bmp"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *completePath = [documentsDirectory stringByAppendingPathComponent:fileName];
[[NSData dataWithData:data] writeToFile:completePath atomically:YES];
return [NSData dataWithData:data];
}
@end
转之前图片:
转后bmp图片: