2

I'm rendering UIImage in the - drawRect: and placing it in the passed in CGRect by calling drawInRect: rect on the UIImage. It automatically resizes and stretches itself, which I don't want. How can I keep it the original size?

UIImage* left = [UIImage imageNamed: @"Map_info_bubble_left"];
UIImage* center = [UIImage imageNamed: @"Map_info_bubble_center"];
UIImage* right = [UIImage imageNamed: @"Map_info_bubble_right"];

UIImage* leftCapStretched = [left resizableImageWithCapInsets: UIEdgeInsetsMake(0, 20, 0, 0)];
UIImage* rightCapStretched = [right resizableImageWithCapInsets: UIEdgeInsetsMake(0, 0, 0, 15)];   

CGFloat fullWidth = leftCapStretched.size.width + center.size.width + rightCapStretched.size.width;

UIGraphicsBeginImageContextWithOptions(CGSizeMake(fullWidth, center.size.height), NO, 0);
[leftCapStretched drawAtPoint: CGPointMake(0, 0)];
[center drawAtPoint: CGPointMake(leftCapStretched.size.width, 0)];
[rightCapStretched drawAtPoint: CGPointMake(left.size.width + center.size.width, 0)];
UIImage* callout = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

[callout drawInRect: rect];

Essentially what is happening is the center image should remain the original size while the caps stretch, but the center is stretching and the caps don't stretch enough. I noticed that if I multiply fullWidth by 2, it shrinks the center closer to what it should be.

EDIT: More simply asked after reading the documentation a bit more, is there a way to prevent the UIImage from scaling to fit the rect when drawInRect: is called?

Chris
  • 7,270
  • 19
  • 66
  • 110

1 Answers1

4

This can be done simply by passing the images size property to drawInRect:

e.x:

[myImage drawInRect:CGRectMake(someOriginX, someOriginY, myImage.size.width, myImage.size.height)];
Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281