绘制屏幕外的彩色正方形将返回灰度图像 [英] Drawing an offscreen coloured square returns a greyscale image
问题描述
我早些时候问过这个问题,现在正在尝试探索制作屏幕外图像的想法。
出问题了-我想也许是色彩空间?我已经反复地编写代码,直到最终我只用几行就可以证明问题。
我有一个带imageview的视图(称为iv )和一个按钮,按下该按钮时会调用 push
-(UIImage *)块:(CGSize )size {
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColor(context,CGColorGetComponents([UIColor redColor] .CGColor));
CGContextFillRect(上下文,CGRectMake(0,0,size.width,size.height));
UIImage * result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
的返回结果;
}
-(IBAction)推送{
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,未分配内存!);
返回NULL;
}
context = CGBitmapContextCreate(bitmapData,
size.width,
size.height,
8,//每个组件的位
bitmapBytesPerRow,
colorSpace,
kCGImageAlphaPremultipliedLast);
// CGContextSetAllowsAntialiasing(context,NO);
if(context == NULL){
free(bitmapData);
fprintf(stderr,未创建上下文!);
返回NULL;
}
CGColorSpaceRelease(colorSpace);
返回上下文;
}
-(UIImage *)块:(CGSize)size {
UIGraphicsBeginImageContext(size);
CGContextRef context = [self createBitmapContextOfSize:size];
CGContextSetFillColor(context,CGColorGetComponents([UIColor blueColor] .CGColor));
CGContextFillRect(上下文,CGRectMake(0,0,size.width,size.height));
UIImage * result = [UIImage imageWithCGImage:CGBitmapContextCreateImage(context)];
CGContextRelease(context);
UIGraphicsEndImageContext();
的返回结果;
}
具有相同的结果。灰色框(在这种情况下不是蓝色框)。
我确定是否可以使此框正常工作。
如果您有CGColor对象,只需使用 CGContextSetFillColorWithColor
函数。。 p>
I asked this question earlier and am now trying to explore the idea of making an offscreen image.
Something is going wrong - I think maybe with colour space? I've boiled the code down and down and down, until I eventually can demonstrate the problem with just a few lines.
I have a view with an imageview (called iv) and a button, which when pushed, calls "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)];
}
The issue here is that instead of drawing a 50x50 block in the specified colour, it draws it in a shade of grey, which leads me to think its a colour space error.
I tried the same thing with a Bitmap Context using
- (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;
}
with identical results. Grey boxes not (in this case) a blue box.
I'm sure if I can get this to work everything else will follow.
If you have a CGColor object, just use the CGContextSetFillColorWithColor
function.
这篇关于绘制屏幕外的彩色正方形将返回灰度图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!