使用drawAtPoint绘制图像(可可触摸)

发布于 2024-09-24 06:28:59 字数 278 浏览 2 评论 0原文

我是 iOS 的 Objective-C 编程新手。我正在努力完成一个非常简单的任务,用代码绘制图像(不仅仅是将其包含在“界面生成器”中)。这是我尝试将图像放入视图的代码部分:

UIImage *image = [UIImage imageNamed:@"Note.png"];

[image drawAtPoint:CGPointZero];

简单。我还尝试了一些保留和释放命令,甚至尝试在旧视图之上包含第二个视图,以绘制图像。但没有运气。

谢谢,约翰。

I'm new to objective-C programming for iOS. I'm struggling with a really simple task, drawing an image with code (not just including it in 'interface builder'). Here's the part of my code where I'm trying to put my image into the view:

UIImage *image = [UIImage imageNamed:@"Note.png"];

[image drawAtPoint:CGPointZero];

Simple. I have also tried with some retain and release commands, and even tried to include a second view on top of the old, to draw the image in. Without luck.

Thanks, John.

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

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

发布评论

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

评论(2

如此安好 2024-10-01 06:28:59

你在哪里做这个? drawAtPoint 需要有一个有效的当前绘图上下文。最常见的是,您应该从某个视图的 drawRect 方法内部调用它。

如果这是正确的,请检查:

  • 图像本身实际上是有效的,
  • 它的 size 有意义,
  • 它可以使用 [image drawInRect:[yourViewbounds]] 之类的内容进行合理绘制,

如果所有这些都是那么,你就遇到了一个有趣的问题。否则,要么你的图像很糟糕,要么你画的时间不对。

Where are you doing this? drawAtPoint will require there to be a valid current drawing context. Most commonly you should call it from inside some view's drawRect method.

If that's correct, check that:

  • the image itself is actually valid
  • its size makes sense
  • it draws sensibly with something like [image drawInRect:[yourView bounds]]

If all those are so, then you've got an interesting problem. Otherwise, either your image is duff or you're drawing at the wrong time.

顾忌 2024-10-01 06:28:59

您发布的代码需要放入 drawRect。就在调用drawRect之前,视图的该部分被有效地删除,这意味着您需要重新绘制作为参数传递的矩形中的任何内容。

(注意:如果您更看重代码的简单性而不是性能,则可以在drawRect中绘制整个视图,而不是仅绘制请求绘制的部分。)

The code you posted needs to go in drawRect. Just before drawRect is called, that part of your view is effectively erased, meaning you need to redraw whatever is in the rect that is passed as an argument.

(Note: if you value code simplicity over performance, you can draw the entire view in drawRect rather than only drawing the part that was requested to be drawn.)

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