作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想将图像从一种格式(.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/
我是一名优秀的程序员,十分优秀!