无法在 UIView::drawRect 中绘制 UImage
我知道这似乎是一个简单的任务,这就是为什么我不明白为什么我无法渲染图像。
当我设置 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,验证 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);