绘制离屏彩色方形返回灰度图像(Drawing an offscreen coloured squar

2019-10-20 04:31发布

我问前面这个问题 ,我现在在努力探索使屏幕外图像的想法。

事情错了 - 我想,也许与色彩空间? 我煮的代码下来,下来,下来,直到我终于可以证明,只需几行的问题。

我有一个ImageView的(称为iv)和一个按钮,当推的图,所说的“推”


- (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)];
}

这里的问题是,而不是在指定的颜色绘制一个50×50块,它在吸引它在灰色的阴影,这使我想到了一个色彩空间的错误。

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


- (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;
}

用相同的结果。 灰盒的话(这种情况下)一个蓝色的盒子。

我敢肯定,如果我能得到这个工作,一切将随之而来。

Answer 1:

如果你有一个CGColor对象,只是使用的CGContextSetFillColorWithColor功能 。



Answer 2:

找到了答案

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

未能在上述情况下(但在drawRect中使用时的作品!)

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

正常工作。

我认为,这显示无知,我的整个区域。 如果有人可以解释,或者在正确的文件指向我,我将非常感激。



Answer 3:

这是发生,因为CGContextSetFillColor()没有影响,除非CGContextSetFillColorSpace()已在上下文的生存某些时候首先被调用。

无论CGContextSetFillColorWithColor()CGContextSetRGBFillColor()也设置填充颜色空间,这就是为什么那些工作。

正如彼得Hosey说,在你的例子做的最好的事情是CGContextSetFillColorWithColor()



文章来源: Drawing an offscreen coloured square returns a greyscale image