ios jpg图片转16位bmp图片时,能转成16位,但是图片结果不一样。

ios jpg图片转16位bmp图片时,能转成16位,但是图片结果不一样。

#import "UIImage+BitmapData.h"
# pragma pack(push, 1)
typedef struct s_bitmap_header
{
    UInt16 fileType;
    UInt32 fileSize;
    UInt16 reserved1;
    UInt16 reserved2;
    UInt32 bitmapOffset;
    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 :(UIImage *) img
{
    NSData          *bitmapData = nil;
 //   CGImageRef      image = self.CGImage;
    CGImageRef      image = img.CGImage;
    CGContextRef    context = NULL;
    CGColorSpaceRef colorSpace;
    UInt8           *rawData =NULL;
    size_t bitsPerPixel = 16;
    size_t bitsPerComponent = 5;
    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)
    {
       rawData = (UInt8 *)calloc(bufferLength, sizeof(UInt8));
        if (rawData)
        {
           
            CGBitmapInfo bitmapInfo = kCGImageByteOrder16Little | kCGImageAlphaNoneSkipFirst;
            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 = self.CGImage;
    CGImageRef image = img.CGImage;
    UInt32     width = (UInt32)CGImageGetWidth(image);
    UInt32     height = (UInt32)CGImageGetHeight(image);
    t_bitmap_header header;
    //bitmapfileheader
    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)之间的偏移量
    //bitmapinfoheader
    header.headerSize = 0x00000028;// infoHeader这个头文件的大小
    header.width = width;//图像宽度,单位为像素
    header.height = height;//图像高度,单位为像素
    header.colorPlanes = 0x0001;//默认1
    header.bitsPerPixel = 0x0010;//每个像素比特数
    header.compression = 0x00000000;
    //压缩方式,0表示不压缩,1表示RLE8压缩,2表示RLE4压缩,3表示每个像素值由指定的掩码决定
    header.bitmapSize = width * height * 3;//图片大小
    header.horizontalResolution = 0x00000B13;
    //0x00000000
    //水平分辨率,单位像素/m
    header.verticalResolution = 0x00000B13;
    //垂直分辨率,单位像素/m   //0x00000000
    header.colorsUsed = 0x00000000;
    //BMP图像使用的颜色,0表示使用全部颜色
    header.colorsImportant = 0x00000000;
    //重要的颜色数,此值为0时所有颜色都重要
    NSLog(@"----bitmapFileHeaderData-----%@",[NSData dataWithBytes:&header length:sizeof(t_bitmap_header)]);
    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];
    NSLog(@"----completePath-----%@",completePath);
    [[NSData dataWithData:data] writeToFile:completePath atomically:YES];
    NSLog(@"--bitmapdata--%@",[NSData dataWithData:data]);
    return [NSData dataWithData:data];
}
@end

#import "UIImage+BitmapData.h"
int main(int argc, char *argv[]) {
    @autoreleasepool {
   
        UIImage *image = [UIImage imageNamed:@"11.png"];
        UIImage *ub  = [[UIImage alloc] init];
        [ub bitmapDataWithFileHeader:image];
    }
   return 0;
}

转前:

img


转后:

img

应该是分辨率太低了,可以看看改下这段里的值,可以多试几次不同值😅

size_t bitsPerPixel = 16;
    size_t bitsPerComponent = 5;
    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;