0

所以我试图让图像出现在特定点。我看过一些关于 drawInRect 的东西,但我不确定为什么这段代码不能与 drawAtPoint 一起使用。这是我正在使用的。

- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed:@"icon.png"];
CGPoint kitty;
kitty.x = 144;
kitty.y = 144;
[image drawAtPoint:(CGPoint)kitty];

我也用 cGPointMake 试过这个,然后把坐标放在后面。这两种方法都不会显示图像,但也不会引发错误消息。我错过了什么?

4

2 回答 2

0

为了让它工作,我需要创建一个 UIView 的子类并将其放入其中:

- (void)drawRect:(CGRect)rect
{
[[UIImage imageNamed:@"icon.png"] drawAtPoint:CGPointMake(curPoint.x,curPoint.y) ];
}

我缺少的另一件事是我在 ViewController.m 文件中使用的 setNeedsDisplay 方法。

于 2012-10-15T01:56:13.017 回答
0

使用 UIImageView。它是处理图像的特定对象。

// 在伪代码中:

UIImage* anImage = [UIImage imageNamed: @"myImage.png"]
UIImageView* imageView = [[UIImageView alloc] initWithImage: anImage];
(UIView*)[self addChild: imageView];
imageView.center = (CGPoint){10, 20};

drawrect:应该用于特定任务,它是一个模板模式,你应该使用它来更新一些 GUI 逻辑。阅读苹果文档以获取更多详细信息(顺便说一下,我很少使用它)

于 2012-10-10T07:15:25.243 回答