当调用drawInRect:时,防止UIImage调整大小

时间:2012-08-17 14:16:22

标签: objective-c ios core-graphics drawrect

我正在- drawRect:中渲染UIImage,并通过调用UIImage上的CGRect将其放入传入的drawInRect: rect中。它自动调整大小和伸展自己,这是我不想要的。如何保持原始尺寸?

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

基本上发生的事情是,当帽子伸展时,中心图像应该保持原始尺寸,但是中心是伸展的,帽子不够伸展。我注意到如果我将fullWidth乘以2,它会使中心缩小到它应该的位置。

编辑:更简单地在阅读文档之后再问一下,有没有办法阻止UIImage在调用drawInRect:时缩放以适应矩形?

1 个答案:

答案 0 :(得分:4)

这可以通过将images size属性传递给drawInRect:

来完成

e.x:

[myImage drawInRect:CGRectMake(someOriginX, someOriginY, myImage.size.width, myImage.size.height)];