【问题标题】:image not scaled using UIImage drawInRect? (see code attached)图像未使用 UIImage drawInRect 缩放? (见附件代码)
【发布时间】:2011-06-30 03:56:04
【问题描述】:

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

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

还请注意,我尝试同时使用“self.window.frame”和“self.frame”,因为我不适合使用哪一个。他们都给出了不同的渲染,但是图像缩放的都不是正确的。

另外我真的很喜欢“Scale To Fit”类型的缩放,但是我不确定在使用“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];
}

【问题讨论】:

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


    【解决方案1】:

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

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

    通常您会期望传递给drawRect: 方法的rect 参数等效于bounds 属性,但它总是不会如此,因此依赖它是不正确的。

    【讨论】:

      猜你喜欢
      • 2012-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-13
      • 2017-04-28
      相关资源
      最近更新 更多