1

我想将图像从一种格式(.png)转换为其他图像格式(.img 格式)。我能够检索和修改相同图像格式的 rgba 值。是否需要做任何其他事情才能将其转换为其他图像格式?

我创建了一个空位图并想绘制该位图的图像。

CGImageRef cgimage = image.CGImage;

size_t width  = CGImageGetWidth(cgimage);
size_t height = CGImageGetHeight(cgimage);

size_t bytesPerRow = CGImageGetBytesPerRow(cgimage);
size_t bytesPerPixel = CGImageGetBitsPerPixel(cgimage);
size_t bitsPerComponent = CGImageGetBitsPerComponent(cgimage);
size_t bytes_per_pixel = bytesPerPixel / bitsPerComponent;

CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(cgimage);

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = malloc(height * width * 4);
memset(rawData, 0, height * width * 4);

CGContextRef context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, bitmapInfo);

CGContextDrawImage(context, CGRectMake(0, 0, width, height), image.CGImage);

iOS 中是否有任何函数可以用修改后的 rgba 值填充输出位图。

4

1 回答 1

3

它看起来像这样:

UIImage *image = self.theImage;
CGImageRef imageRef = [image CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
NSMutableData *data = [[NSMutableData alloc] initWithCapacity:height * width * 4];
unsigned char *rawData = data.mutableBytes;
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height,
                                             bitsPerComponent, bytesPerRow, colorSpace,
                                             kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);

CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);

int byteIndex = (bytesPerRow * 0) + 0 * bytesPerPixel;

如果要操作数据,请遍历byteIndex. 希望这就是你要找的。

于 2013-04-15T11:15:38.557 回答