我在- 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,则会使中心缩小到应有的水平。

编辑:在阅读了更多文档后,更简单地问,有没有一种方法可以防止在调用drawInRect:时将UIImage缩放以适合rect?

最佳答案

只需将images size属性传递给drawInRect:即可完成

e.x:

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

关于objective-c - 防止在调用drawInRect:时调整UIImage的大小,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12007519/

10-09 02:16