gpt4 book ai didi

ios - 如何在 iOS 中创建和检索新的位图

转载 作者:行者123 更新时间:2023-11-28 22:35:58 24 4
gpt4 key购买 nike

我想将图像从一种格式(.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 值填充输出位图。

最佳答案

它看起来像这样:

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 .希望这就是您要找的。

关于ios - 如何在 iOS 中创建和检索新的位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16013390/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com