有人问过类似的问题,但它们总是包含“使用 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 结构?我在哪里可以找到关于如何用值填充这个结构的文档?