【发布时间】: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