使用drawAtPoint绘制图像(可可触摸)
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你在哪里做这个?
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'sdrawRect
method.If that's correct, check that:
size
makes sense[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.
您发布的代码需要放入 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.)