【问题标题】:Drawing an offscreen coloured square returns a greyscale image绘制屏幕外彩色方块会返回灰度图像
【发布时间】:2010-03-05 00:30:00
【问题描述】:

我问过this question earlier,现在我正在尝试探索制作屏幕外图像的想法。

出了点问题 - 我想可能是色彩空间?我已经将代码精简了一遍又一遍,直到我最终可以用几行代码来演示问题。

我有一个带有 imageview(称为 iv)和一个按钮的视图,当按下它时,它会调用“push”


- (UIImage *) block:(CGSize)size {
    UIGraphicsBeginImageContext(size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColor(context, CGColorGetComponents([UIColor redColor].CGColor));
    CGContextFillRect (context, CGRectMake(0, 0, size.width, size.height));
    UIImage *result =  UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext();
    return result;  
}


- (IBAction) push {
    self.iv.image = [self block:CGSizeMake(50.0,50.0)];
}

这里的问题是,它不是以指定颜色绘制一个 50x50 的块,而是以灰色阴影绘制它,这让我认为这是一个色彩空间错误。

我使用位图上下文尝试了同样的事情


- (CGContextRef) createBitmapContextOfSize:(CGSize) size {
    CGContextRef    context = NULL;
    CGColorSpaceRef colorSpace;
    void *          bitmapData;
    int             bitmapByteCount;
    int             bitmapBytesPerRow;

    bitmapBytesPerRow   = (size.width * 4);
    bitmapByteCount     = (bitmapBytesPerRow * size.height);

    colorSpace = CGColorSpaceCreateDeviceRGB();
    bitmapData = malloc( bitmapByteCount );
    if (bitmapData == NULL) {
        fprintf (stderr, "Memory not allocated!");
        return NULL;
    }
    context = CGBitmapContextCreate (bitmapData,
                                     size.width,
                                     size.height,
                                     8,      // bits per component
                                     bitmapBytesPerRow,
                                     colorSpace,
                                     kCGImageAlphaPremultipliedLast);
    //CGContextSetAllowsAntialiasing (context,NO);
    if (context== NULL) {
        free (bitmapData);
        fprintf (stderr, "Context not created!");
        return NULL;
    }
    CGColorSpaceRelease( colorSpace );
    return context;
}

- (UIImage *) block:(CGSize)size {  
    UIGraphicsBeginImageContext(size);
    CGContextRef context = [self createBitmapContextOfSize:size];
    CGContextSetFillColor(context, CGColorGetComponents([UIColor blueColor].CGColor));
    CGContextFillRect (context, CGRectMake(0, 0, size.width, size.height));
    UIImage *result = [UIImage imageWithCGImage: CGBitmapContextCreateImage (context)];
    CGContextRelease(context);
    UIGraphicsEndImageContext();
    return result;
}

结果相同。灰色框不是(在这种情况下)蓝色框。

我敢肯定,如果我可以让它工作,其他一切都会随之而来。

【问题讨论】:

    标签: iphone cocoa-touch core-graphics


    【解决方案1】:

    如果您有 CGColor 对象,只需使用 the CGContextSetFillColorWithColor function

    【讨论】:

    • 问题是我使用 [UIColor 不管].CGColor 来制作我的 CGColor。由于我无法解释的原因,这在 CGContextSetRGBFillColor 起作用的地方不起作用。因此我怀疑(但我还没有尝试过)CGContextSetFillColorWithColor 会以同样的方式失败。
    • 我不同意。我相信这是 CGContextSetFillColor 预期和 CGColorGetComponents 返回的不匹配。例如,如果上下文的初始颜色空间是一个白色空间(这将匹配 PostScript 的行为),那么 CGContextSetFillColor 将只读取一个或两个(白色和 alpha)分量,而不知道你给了它两个。使用CGContextSetFillColorWithColor 也会设置色彩空间,防止不匹配。
    • 这肯定符合我的观察。我会测试它并反馈。
    【解决方案2】:

    找到答案

    CGContextSetFillColor(context, CGColorGetComponents([UIColor redColor].CGColor));

    在上述情况下失败(但在 drawRect 中使用时有效!)

    CGContextSetRGBFillColor(context, 1, 0, 0, 1);

    工作正常。

    我认为这表明我对整个领域的无知。如果有人可以解释或指出正确的文档,我将不胜感激。

    【讨论】:

      【解决方案3】:

      发生这种情况是因为 CGContextSetFillColor() 没有任何效果,除非 CGContextSetFillColorSpace() 在上下文生命周期的某个时间点被首先调用。

      CGContextSetFillColorWithColor()CGContextSetRGBFillColor() 都设置了填充颜色空间,这就是它们起作用的原因。

      正如 Peter Hosey 所说,在您的示例中最好的做法是 CGContextSetFillColorWithColor()

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-10-06
        • 2015-11-20
        • 1970-01-01
        • 2013-07-28
        • 2013-04-06
        • 1970-01-01
        • 2011-12-28
        • 1970-01-01
        相关资源
        最近更新 更多