2

我正在渲染 UIImage,并通过调用UIImage- drawRect:将其放入传入的文件中。它会自动调整大小并自行拉伸,这是我不想要的。我怎样才能保持它原来的大小?CGRectdrawInRect: 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];

本质上发生的事情是中心图像在大写拉伸时应保持原始大小,但中心正在拉伸并且大写拉伸不够。我注意到,如果我乘以fullWidth2,它会将中心缩小到更接近应有的位置。

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

4

1 回答 1

4

这可以简单地通过将图像大小属性传递给drawInRect:

前任:

[myImage drawInRect:CGRectMake(someOriginX, someOriginY, myImage.size.width, myImage.size.height)];
于 2012-08-17T14:50:01.960 回答