开发者

image not scaled using UIImage drawInRect? (see code attached)

开发者 https://www.devze.com 2023-03-16 03:32 出处:网络
why开发者_Python百科 is the image I\'m trying to render in a custom UIView not being fit into window area to scale?

why开发者_Python百科 is the image I'm trying to render in a custom UIView not being fit into window area to scale?

The code below is from a custom class that inherits from UIView. This view has been placed onto the main view in an XIB file, such that it's not taking up the whole screen.

Also note I tried using both "self.window.frame" as well as "self.frame" as I wasn't such which one to use. They both given different renderings however neither is one where the image is scaled correct.

Also I'm really after the "Scale To Fit" type of scaling, however I'm not sure how you would get this mode when using a "drawInRect" either?

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

Neither did the following work too:

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


Use the bounds property. It will give you the rect in the view's coordinate space. frame is the coordinates in its parent's coordinate space so it will not work.

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

Normally you would expect rect parameter passed to drawRect: method to be equivalent to the bounds property but it always won't be so it would be incorrect to rely on it.

0

精彩评论

暂无评论...
验证码 换一张
取 消