如何将UIImage保存为文档目录中的BMP文件

时间:2012-08-22 15:13:45

标签: ios uiimage core-graphics bmp

已经提出了类似的问题,但它们总是包含答案“使用UIImagePNGRepresentation或UIImageJPEGRepresentation”但这对我不起作用,因为我必须将此文件发送到只接受BMP图像的外部Web服务。

这是我到目前为止实施的

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

这会将文件保存到文档目录,但我无法通过预览打开该文件。

我做错了什么?有更简单的方法吗?

编辑

我现在已经添加了文件头的结构

#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)

我还实现了一个函数来将标题附加到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;    
}

这不起作用,有时由于释放的错误指针而导致的崩溃没有在最后一次memcpy调用上分配,这很奇怪,因为我实际上并没有释放任何数据。当它工作时,它会产生一个无法读取的文件 我是否必须实现bitmapv5header结构?我在哪里可以找到关于如何使用值填充此结构的文档?

1 个答案:

答案 0 :(得分:0)

您不能只保存添加特定标题和调色板的数据。 请查看文件格式:http://en.wikipedia.org/wiki/BMP_file_format