CGLayer 显示错误
我有一个 UIView 子类 (TraceView),宽 200,高 100,我尝试在其上逐像素绘制。 在本例中,我尝试绘制一条 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);
}
I've got a UIView subclass (TraceView) which is 200 wide by 100 tall, and I'm attempting to draw on it pixel by pixel. In this case, I'm trying to draw a horizontal blue line that goes 75% of the way across. Instead, I get two vertical blue lines (one at the edge, one in the middle), that go 75% of the way up. It seems like the CGLayer I use has been rotated 90 degrees and shrunk 50%. What can I do to correct this?
At the top of the .m file:
#define WORLD_WIDTH 200
#define WORLD_HEIGHT 100
#define ABGR_BYTES 4
@implementation TraceView
int theImage[WORLD_WIDTH][WORLD_HEIGHT];
Overriding 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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我已经成功了,愚蠢的我。
如果我将数组视为 theImage[y][x] 而不是 theImage[x][y],则它可以正常工作。
OK, I got it working, silly me.
It works OK if I treat the array as theImage[y][x] rather than theImage[x][y].