gpt4 book ai didi

objective-c - 防止 UIImage 在 - drawInRect : is called 时调整大小

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:49:08 27 4
gpt4 key购买 nike

我在 - 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 缩放以适合矩形?

最佳答案

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

来完成

例如:

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

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com