代码之家  ›  专栏  ›  技术社区  ›  Andiih

绘制屏幕外的彩色正方形将返回灰度图像

  •  1
  • Andiih  · 技术社区  · 15 年前

    我问 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;
    }
    

    结果相同。灰盒不是蓝盒。

    我相信如果我能让这个工作,其他一切都会随之而来。

    3 回复  |  直到 9 年前
        1
  •  4
  •   Peter Hosey    15 年前

    如果有cgcolor对象,只需使用 the CGContextSetFillColorWithColor function .

        2
  •  0
  •   Andiih    15 年前

    找到了答案

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

    在上述场景中失败(但在DrawRect中使用时有效!)

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

    工作正常。

    我想这对我来说是一个无知的领域。如果有人能向我解释或指出正确的文件,我将不胜感激。

        3
  •  0
  •   mrwalker    9 年前

    这是因为 CGContextSetFillColor() 无效,除非 CGContextSetFillColorSpace() 在上下文的生命周期中的某个时刻被首先调用。

    两个 CGContextSetFillColorWithColor() CGContextSetRGBFillColor() 同时设置填充颜色空间,这就是为什么这些工作。

    正如彼得·霍西所说,在你的例子中,最好的做法是 cgContextSetFillColorWithColor() .