CGLayer 显示错误

发布于 2024-07-29 04:24:42 字数 1172 浏览 5 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦境 2024-08-05 04:24:42

好吧,我已经成功了,愚蠢的我。

如果我将数组视为 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].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文