无法在 UIView::drawRect 中绘制 UImage

发布于 2024-09-04 00:27:04 字数 719 浏览 0 评论 0原文

我知道这似乎是一个简单的任务,这就是为什么我不明白为什么我无法渲染图像。

当我设置 UIView 时,我执行以下操作:

myUiView.backgroundColor = [UIColor clearColor];
myUiView.opaque = NO;

我在 UIView 的 init 函数中创建并保留 UIImage:

image = [[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"png"]] retain];

然后我的 drawRect 如下所示:

- (void) drawRect:(CGRect) rect
{
    [image drawInRect:self.bounds];
}

最终我将通过位图上下文操作该 UIImage,然后在 drawRect 创建脱离上下文的 CGImage 并渲染它,但现在我只是试图让它渲染一个已知的图像。

我一直在研究这个网站以及文档。我沿着 CG 路径尝试按照其他人发布的大量示例使用 CGContextDrawImage 进行绘制,但这也不起作用。

所以我又回到了似乎最直接的绘制图像的方法,但它不起作用。

任何帮助将不胜感激。

提前致谢。

I know this seems like a simple task, which is why I don't understand why I can't get the image to render.

When I set up my UIView, I do the following:

myUiView.backgroundColor = [UIColor clearColor];
myUiView.opaque = NO;

I create and retain the UIImage in the init function of my UIView:

image = [[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"png"]] retain];

then my drawRect looks like this:

- (void) drawRect:(CGRect) rect
{
    [image drawInRect:self.bounds];
}

Ultimately I'll be manipulating that UIImage via bitmap context, and then in drawRect create a CGImage out of the context, and render that, but for now I'm just trying to get it rendering a known image.

I've been digging through this site, as well as the documentation. I've gone down the CG path and tried drawing it with CGContextDrawImage by following the numerous examples other people have posted, but that didn't work either.

So I've come back to what seems to be the most straightforward way to draw an image, but it isn't working.

Any help would be greatly appreciated.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

浪推晚风 2024-09-11 00:27:04

首先,验证 self.bounds 的大小和位置是否符合您的要求。如果大小为 {0,0},则不会显示任何内容。使用此函数进行检查:

NSLog(@"%@", NSStringFromCGRect(self.bounds));

还要确保图像不为零:

NSLog(@"%@", image );

First of all, verify that the size and position of self.bounds are what you want them to be. If the size is {0,0} nothing will display. Check using this function:

NSLog(@"%@", NSStringFromCGRect(self.bounds));

Also make sure that the image is not nil:

NSLog(@"%@", image);

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