我有一个 200 宽 x 100 高的 UIView 子类(TraceView),我正试图逐个像素地绘制它。在这种情况下,我试图绘制一条横穿 75% 的水平蓝线。相反,我得到两条垂直的蓝线(一条在边缘,一条在中间),向上延伸了 75%。我使用的 CGLayer 好像旋转了 90 度,缩小了 50%。我能做些什么来纠正这个问题?
在 .m 文件的顶部:
#define WORLD_WIDTH 200
#define WORLD_HEIGHT 100
#define ABGR_BYTES 4
@implementation TraceView
int theImage[WORLD_WIDTH][WORLD_HEIGHT];
覆盖drawRect:
- (void)drawRect:(CGRect)rect {
CGContextRef theViewContext = UIGraphicsGetCurrentContext();
CGLayerRef theCGLayer = CGLayerCreateWithContext(theViewContext, rect.size, nil);
CGContextRef theCGLayerContext = CGLayerGetContext(theCGLayer);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef bitmapContext = CGBitmapContextCreate(theImage, WORLD_WIDTH, WORLD_HEIGHT, 8, WORLD_WIDTH * ABGR_BYTES, colorSpace, kCGImageAlphaPremultipliedLast);
// is ABGR
for(int i=0;i<150;i++) {
theImage[i][0]=0xFFFF0000;
}
CGImageRef image = CGBitmapContextCreateImage(bitmapContext);
CGContextDrawImage(theCGLayerContext, rect, image);
CGContextDrawLayerInRect(theViewContext, rect, theCGLayer);
}