我想通过使用cgcontextfillXXX绘图在iOS中生成灰色图像,但失败了

我想通过使用cgcontextfillXXX绘图在iOS中生成灰色图像,但失败了,ios,image,Ios,Image,代码在上面。它不能像我预期的那样工作。我不熟悉iOS CoreGraphic框架。请参阅中的CGColorCreate的语法 示例代码为: CGRect rect = CGRectMake(0, 0, 100, 100); float white[1] = {0.0f}; float gray[1] = {1.0f}; CGContextSetFillColorSpace(bitmap, colorSpace); CGContextSetFillColorWithColor(bitmap, CG

代码在上面。它不能像我预期的那样工作。我不熟悉iOS CoreGraphic框架。

请参阅中的
CGColorCreate
的语法

示例代码为:

CGRect rect = CGRectMake(0, 0, 100, 100);
float white[1] = {0.0f};
float gray[1] = {1.0f};
CGContextSetFillColorSpace(bitmap, colorSpace);
CGContextSetFillColorWithColor(bitmap, CGColorCreate(colorSpace, white));
CGContextClearRect(bitmap, rect);
CGContextSetFillColorWithColor(bitmap, CGColorCreate(colorSpace, gray));
CGContextFillEllipseInRect(bitmap, rect);
CGImageRef imgRef = CGBitmapContextCreateImage(bitmap);
UIImage *image  = [[[UIImage alloc] initWithCGImage:imgRef] autorelease];
self.imageView.image = image;
CGContextRelease(bitmap);
CGColorSpaceRelease(colorSpace);
注意:您必须根据自己的意愿定义
r、g、b
,它们都是
CGFloat
类型

CGFloat components[]={r, g, b, 1.f};
drawColor=CGColorCreate(colorSpace, components);
CGColorSpaceRelease(colorSpace);