Objective c 目标C-将UIImage另存为BMP文件
也有人问过类似的问题,但这些问题的答案总是“使用UIImagePNGRepresentation或UIImageJPEGRepresentation”,这对我来说是行不通的。我需要将文件保存为位图 据我所知,UIImage数据已经是位图了。在这种情况下,我不应该创建位图。还是我?我发现有人发了帖子,但我不确定答案到底是什么,因为我不清楚。我甚至创建了一个字符指针数组,但我仍然不知道如何将其保存为位图。有没有人可以帮助我理解如何将UIImage保存为位图?或者告诉我步骤,我会研究步骤 多谢各位Objective c 目标C-将UIImage另存为BMP文件,objective-c,c,bitmap,uiimage,Objective C,C,Bitmap,Uiimage,也有人问过类似的问题,但这些问题的答案总是“使用UIImagePNGRepresentation或UIImageJPEGRepresentation”,这对我来说是行不通的。我需要将文件保存为位图 据我所知,UIImage数据已经是位图了。在这种情况下,我不应该创建位图。还是我?我发现有人发了帖子,但我不确定答案到底是什么,因为我不清楚。我甚至创建了一个字符指针数组,但我仍然不知道如何将其保存为位图。有没有人可以帮助我理解如何将UIImage保存为位图?或者告诉我步骤,我会研究步骤 多谢各位
编辑:
我找到了(带有代码)用于将图像转换为位图,但返回的位图是无符号字符*。要么我走对了路,要么我走错了方向。现在我要看看是否可以将未签名字符*写入.bmp文件。下面是我为UIImage拼凑的类别,它允许我访问原始BGRA数据以及准备使用适当位图头写入磁盘的数据
@interface UIImage (BitmapData)
- (NSData *)bitmapData;
- (NSData *)bitmapFileHeaderData;
- (NSData *)bitmapDataWithFileHeader;
@end
# pragma pack(push, 1)
typedef struct s_bitmap_header
{
// Bitmap file header
UInt16 fileType;
UInt32 fileSize;
UInt16 reserved1;
UInt16 reserved2;
UInt32 bitmapOffset;
// DIB Header
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
{
NSData *bitmapData = nil;
CGImageRef image = self.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 = kCGBitmapByteOrder32Little | 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
{
CGImageRef image = self.CGImage;
UInt32 width = (UInt32)CGImageGetWidth(image);
UInt32 height = (UInt32)CGImageGetHeight(image);
t_bitmap_header header;
header.fileType = 0x4D42;
header.fileSize = (height * width * 4) + 54;
header.reserved1 = 0x0000;
header.reserved2 = 0x0000;
header.bitmapOffset = 0x00000036;
header.headerSize = 0x00000028;
header.width = width;
header.height = height;
header.colorPlanes = 0x0001;
header.bitsPerPixel = 0x0020;
header.compression = 0x00000000;
header.bitmapSize = height * width * 4;
header.horizontalResolution = 0x00000B13;
header.verticalResolution = 0x00000B13;
header.colorsUsed = 0x00000000;
header.colorsImportant = 0x00000000;
return [NSData dataWithBytes:&header length:sizeof(t_bitmap_header)];
}
- (NSData *)bitmapDataWithFileHeader
{
NSMutableData *data = [NSMutableData dataWithData:[self bitmapFileHeaderData]];
[data appendData:[self bitmapData]];
return [NSData dataWithData:data];
}
@end
可能是复制品,我试试看。谢谢。不,这是使用UIImagePNGRE演示文稿的。我需要将其保存为位图。它首先会保存,但请查看它如何使用CI上下文将其导出为bmp。位图上下文中的数据应该是什么?您好!我试图使用您的代码(经过一些修改)生成一个8位灰度位图,但无法进行适当的更改以使其正常工作。你能帮我吗?谢谢问题发布在这里: