- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我能够找到一种方法来帮助我修改应用程序中 UIImage 中像素的 alpha 值,但我遇到了两个错误(第二个错误肯定是由第一个错误引起的)。但是,我无法弄清楚发生了什么。
我的方法:
- (void)modifyAlpha:(int)x and:(int)y {
CGContextRef ctx;
CGImageRef imageRef = [scratchOffImage CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = malloc(height * width * 4);
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);
// Now your rawData contains the image data in the RGBA8888 pixel format.
int byteIndex = (bytesPerRow * y) + x * bytesPerPixel;
rawData[byteIndex+3] = (char) (255); //Change the pixel value
ctx = CGBitmapContextCreate(rawData,
CGImageGetWidth( imageRef ),
CGImageGetHeight( imageRef ),
8,
CGImageGetBytesPerRow( imageRef ),
CGImageGetColorSpace( imageRef ),
kCGImageAlphaPremultipliedFirst ); //This line causes an error: incorrect colorspace
imageRef = CGBitmapContextCreateImage (ctx);
UIImage* rawImage = [UIImage imageWithCGImage:imageRef];
CGContextRelease(ctx);
scratchOffImage = rawImage;
[scratchOffImageView setImage:scratchOffImage];
free(rawData);
}
错误:
<Error>: CGBitmapContextCreate: unsupported color space.
正在上线:
ctx = CGBitmapContextCreate(rawData, ...
然后是第二个错误:
<Error>: CGBitmapContextCreateImage: invalid context 0x0
正在上线:
imageRef = CGBitmapContextCreateImage (ctx);
我的图像包含在我最初通过使用 PNG-8 和透明度作为格式输出 Photoshop 的“保存为网页”功能而制作的 bundle 中。
当我运行首次使用该函数的示例代码时,示例图像工作正常。然而,我的形象却没有。我该如何调试这个?
有人知道如何调试这个吗?我输入的 PNG 格式可能不正确吗?我怎样才能检查这个?
干杯,布雷特
编辑1:原始源代码来自发现的示例here 。该示例显示了到灰度的转换,而我只需要更改 alpha 值。
编辑 2:我尝试将图像保存为 PNG-8 和 PNG-24,但都没有成功。
最佳答案
PNG-8 使用 8 位索引颜色空间。 Quartz 不支持 CGBitmapContext 的索引颜色空间。 CGBitmapContextCreate
documentation说“请注意,位图图形上下文不支持索引颜色空间。”
您不想在第二次调用 CGBitmapContextCreate
时传递 CGImageGetColorSpace( imageRef )
作为颜色空间,而是希望传递在第一次调用中使用的相同颜色空间 (您的 colorSpace
变量)。
无论如何,甚至没有理由创建第二个CGBitmapContext
。并且您正在泄漏 CGBitmapContextCreateImage 的结果。只需这样做:
- (void)modifyAlpha:(int)x and:(int)y {
CGImageRef imageRef = [scratchOffImage CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = malloc(height * width * 4);
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);
// Now your rawData contains the image data in the RGBA8888 pixel format.
int byteIndex = (bytesPerRow * y) + x * bytesPerPixel;
rawData[byteIndex+3] = (char) (255); //Change the pixel value
imageRef = CGBitmapContextCreateImage (context);
CGContextRelease(context);
UIImage* rawImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
scratchOffImage = rawImage;
[scratchOffImageView setImage:scratchOffImage];
free(rawData);
}
关于iPhone SDK - <错误> : CGBitmapContextCreate: unsupported color space,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8794218/
这就是我创建 UIImage 的方式。我用 calloc 分配了 piOutData。现在当我最后调用 CGImageRelease 时,它会自动释放 piOutData 还是我需要通过 free 手
我正在使用以下代码在我的 iOS 应用程序的 touchesEnded 截取绘图: dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PR
这段代码是我写的: bitmapData = calloc(1, bitmapByteCount ); context = CGBitmapContextCreate (bitmapData,
在我的应用程序中,我需要调整和裁剪一些本地和在线存储的图像。我正在使用Trevor Harmon's tutorial它实现了UIImage+Resize。 在我的 iPhone 4(iOS 4.3.
我迫切需要创建一个没有 alpha channel 的 CGContextRef。不幸的是,使用 Core Graphics,您不能使用 CGBitmapCreateContext 指定 kCGIma
- (UIImage *)roundedCornerImage:(NSInteger)cornerSize borderSize:(NSInteger)borderSize { // If t
我想使用来自 Apple 开发人员页面的示例。 Apple Dev Pages on AVFoundation 现在,我按照文档说明使用了 imageFromSampleBuffer:sampleBu
几天来我一直在为撤消功能苦苦挣扎,我真的不知道如何解决这个问题。 我在使用自定义 UIimageView 的 UIViewController 上制作绘图功能。 问题是当我触发撤消功能时,之前的绘图没
我有以下代码来创建新的位图图形上下文。 NSLog(@"newRect Width %f",newRect.size.width); NSLog(@"newRect Height %f",newRec
在 IOS 下,我试图通过绘制到通过 CGBitmapContextCreate 创建的上下文来创建图像。以下是我正在使用的代码(类似于我找到的一些示例),但是当我调用它时,没有绘制任何内容。请注意,
我正在尝试在后台线程中更改图像的颜色。 Apple 文档说 UIGraphicsBeginImageContext 只能从主线程调用,我正在尝试使用 CGBitmapContextCreate: co
CGBitmapContext 在什么情况下会分配失败?我有一个 TableView ,它有多个 View 选项。用户可以看到一个只有预览的小表格单元格,每行一个较大的预览,或每行两个并排预览。前两个
我手动创建了一个 CGBitmapContext: bitmapContext = CGBitmapContextCreate( myImageData, width, height, 8, 4 *
创建位图上下文时出现此错误: CGBitmapContextCreate:不支持的参数组合:8 个整数位/组件; 24 位/像素;三分量色彩空间; kCGImageAlphaNone; 7936 字节
我有一个方法,需要逐像素解析一堆大型 PNG 图像(每个 PNG 都是 600x600 像素)。它似乎在模拟器上工作得很好,但在设备(iPad)上,我在某些内部内存复制功能中得到了 EXC_BAD_A
CGContextRef context = CGBitmapContextCreate(nil, width, //if width More than 6002/4
我正在尝试创建一个 8 位灰度上下文,如下所示: CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray(); CGContextRef c
我正在使用以下代码(来自博客文章)来调整图像大小 if (inImage.size.width <= inImage.size.height) { // Portrait ratio
我正在使用 quartz2d 生成图像,我想将其用作 opengl 纹理。棘手的部分是我希望每个像素使用尽可能少的位,因此我创建 cgContext 如下: int bitsPerComponent
我使用以下代码来显示 32 位灰度图像。即使我明确地将每个像素设置为 4294967297(应该是白色),最终结果始终是黑色。我在这里做错了什么?该图像只有 64x64 像素。 CGColorSpac
我是一名优秀的程序员,十分优秀!