Greg
Greg

Reputation: 34828

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

why 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];
}

Upvotes: 0

Views: 2578

Answers (1)

Deepak Danduprolu
Deepak Danduprolu

Reputation: 44633

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.

Upvotes: 4

Related Questions