我有一个200宽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);
}
答案 0 :(得分:1)
如果我将数组视为图像[y] [x]而不是图像[x] [y],则可以正常工作。