OStack程序员社区-中国程序员成长平台

标题: objective-c - 防止 UIImage 调整大小时 - drawInRect : is called [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 22:29
标题: objective-c - 防止 UIImage 调整大小时 - drawInRect : is called

我在 -drawRect: 中渲染 UIImage,并通过在 UIImage 上调用 drawInRect: rect 将其放置在传入的 CGRect 中.它会自动调整大小并自行拉伸(stretch),这是我不想要的。我怎样才能保持它原来的大小?

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

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

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



Best Answer-推荐答案


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

来完成

e.x:

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

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






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4