iPhone SDK - <错误> : CGBitmapContextCreate: unsupported color space

标签 iphone objective-c

我能够找到一种方法来帮助我修改应用程序中 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/

相关文章:

objective-c - 升级到 iOS 6 和 RootViewController 方向不正确

objective-c - 如何确定 Mac OS X 10.6 中的内核位数?

objective-c - 一个 unsigned unsigned long 的两倍总是一个 double 吗?

ios - 如何将 UISearchBar 文本字段默认占位符 "Search"更改为 "Enter city name..."

ios - UIImagePickerControllerOriginalImage 收到内存警告并崩溃

ios - 自动布局无法按预期布局按钮?

iphone - 有没有使用 GData API 将视频上传到 youtube iOS 应用程序的教程?

ios - CGRectContainsPoint 不返回 YES 但在模拟器上该点在图像中

iphone - 是否可以通过 App Store 分发 iPhone Web 应用程序?

iphone - CFWriteStreamSetProperty 为 kCFStreamPropertySSLSettings 返回 false