How do i save an UIImage as a BMP file in document

2019-06-11 09:18发布

问题:

Similar questions has been asked but they always contain the answer "use UIImagePNGRepresentation or UIImageJPEGRepresentation" But that wont work for me since i have to send this file to an external webservice that only accepts BMP images.

This is what i have implemented so far

-(NSData*)getBitmapRepresentationFromUIImage:(UIImage*)img{

CGImageRef cgiImg =[img CGImage];
CGColorSpaceRef colorSpaceRef = CGImageGetColorSpace(cgiImg);    

size_t bitsPerComponent = CGImageGetBitsPerComponent(cgiImg);
size_t bytesPerRow      = CGImageGetBytesPerRow(cgiImg);
size_t dataSize         = bytesPerRow * CGImageGetHeight(cgiImg);

void* imgBuf = malloc(dataSize);

CGContextRef bmpContext = CGBitmapContextCreate(imgBuf,
                                                   CGImageGetWidth(cgiImg),
                                                   CGImageGetHeight(cgiImg),
                                                   bitsPerComponent,
                                                   bytesPerRow,
                                                   colorSpaceRef,
                                                   kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);


CGContextDrawImage(bmpContext, CGRectMake(0, 0, CGImageGetWidth(cgiImg), CGImageGetHeight(cgiImg)), cgiImg);

NSData* dataToReturn  = nil; 




if (imgBuf!=NULL) {

    dataToReturn = [[NSData alloc] initWithBytes:imgBuf length:dataSize];
    free(imgBuf);
    /*debug*/
    [self saveNSDataAsBitmap:dataToReturn fileName:@"signature"];
    /**/
}



return  dataToReturn;


}

-(void)saveNSDataAsBitmap:(NSData*)data fileName:(NSString*)name{    

NSString* fileName = [NSString stringWithFormat:@"%@%@",name,@".bmp"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];    
NSString *completePath = [documentsDirectory stringByAppendingPathComponent:fileName];    
[data writeToFile:completePath atomically:YES];    
}

This saves a file to the documents directory but i cant open that file with preview.

What am i doing wrong ? is there an easier way ?

EDIT

I have now added structs for the file header

#pragma pack(1)
typedef struct 
{
    UInt16 magic;  //specifies the file type "BM" 0x424d
    UInt32 bfSize;  //specifies the size in bytes of the bitmap file
    UInt16 bfReserved1;  //reserved; must be 0
    UInt16 bfReserved2;  //reserved; must be 0
    UInt32 bOffBits;  
} BmpFileHeaderStruct, *BmpFileHeaderStructRef;
#pragma pack(0)

#pragma pack(1)
typedef struct
{   UInt32 biSize;
    int32_t  biWidth;
    int32_t  biHeight;
    UInt16  biPlanes;
    UInt16  biBitCount;
    UInt32 biCompression;
    UInt32 biSizeImage;
    int32_t  biXPelsPerMeter;
    int32_t  biYPelsPerMeter;
    UInt32 biClrUsed;
    UInt32 biClrImportant;
} BmpFileInfoStruct,*BmpFileInfoStructRef;
#pragma pack(0)

I have also implemented a function to attatch a header to the start of imgBuf

-(void*)attatchBmpFileHeaderFor:(void*)imgBuf sizeOfBuff:(size_t)buffSize forImage:(CGImageRef)img{

BmpFileHeaderStruct headerStruct = {0};
headerStruct.magic = 0x424d;
headerStruct.bfSize = buffSize;
headerStruct.bOffBits = (sizeof(BmpFileHeaderStruct)*8) + (sizeof(BmpFileInfoStruct)*8);//hmmm ? 



BmpFileInfoStruct infoStruct = {0};
infoStruct.biSize = sizeof(BmpFileInfoStruct);
infoStruct.biWidth = CGImageGetWidth(img);
infoStruct.biHeight = CGImageGetHeight(img);
infoStruct.biPlanes = 1;
infoStruct.biBitCount = CGImageGetBitsPerPixel(img);
infoStruct.biSizeImage = buffSize;

void * fileBmpBuf = malloc(sizeof(BmpFileInfoStruct)+sizeof(BmpFileHeaderStruct)+sizeof(buffSize));

void *pIter = fileBmpBuf;
memcpy(pIter, &headerStruct, sizeof(BmpFileHeaderStruct));
pIter += sizeof(BmpFileHeaderStruct);
memcpy(pIter, &infoStruct, sizeof(BmpFileInfoStruct));
pIter += sizeof(BmpFileInfoStruct);
memcpy(pIter, imgBuf, buffSize);

return fileBmpBuf;    
}

This does not work , sometimes it crashes due to the error pointer being freed was not allocated on the last memcpy call which is weird since i dont actually free any data at that point. When it works it produces a file that cant be read.
Do i have to implement a bitmapv5header structure instead ? Where can i find documentation on how i should fill this struct with values ?

回答1:

you can't just save the data you have to add a specific header and the palette. Take a look at the file format: http://en.wikipedia.org/wiki/BMP_file_format