图像没有使用UIImage drawInRect缩放? (见附件代码)

时间:2011-06-30 03:56:05

标签: iphone objective-c ios cocoa-touch uiimage

为什么我试图在自定义UIView中渲染的图像不适合缩放的窗口区域?

以下代码来自一个继承自UIView的自定义类。此视图已放置在XIB文件中的主视图中,因此它不占用整个屏幕。

另请注意,我尝试使用“self.window.frame”以及“self.frame”,因为我不喜欢使用哪一个。它们都给出了不同的渲染效果,但也没有一个是图像缩放正确的。

此外,我真的在“缩放至适合”类型的缩放之后,但是我不确定在使用“drawInRect”时你将如何获得此模式?

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    UIImage *altimeterImage = [UIImage imageNamed:@"Atimeter4.png"];
    [altimeterImage drawInRect:self.window.frame];
}

以下工作也没有:

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    UIImage *altimeterImage = [UIImage imageNamed:@"Atimeter4.png"];
    [altimeterImage drawInRect:self.frame];
}

1 个答案:

答案 0 :(得分:4)

使用bounds属性。它将在视图的坐标空间中为您提供矩形。 frame是其父坐标空间中的坐标,因此不起作用。

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    UIImage *altimeterImage = [UIImage imageNamed:@"Atimeter4.png"];
    [altimeterImage drawInRect:self.bounds];
}

通常,您希望传递给rect方法的drawRect:参数等同于bounds属性,但它始终不会因此依赖它而不正确。< / p>