UISegmentedControl图像缩放
如何缩小 UISegmentedControl
中使用的图像?我正在以编程方式创建分段控件:
UISegmentedControl * segmentButton;
segmentButton = [UISegmentedControl segmentedControlWithItems:
[NSArray arrayWithObjects:
[UIImage imageNamed:@"option_one.png"],
[UIImage imageNamed:@"option_two.png"],
nil]];
segmentButton.contentMode = UIViewContentModeScaleToFill;
segmentButton.frame = CGRectMake(10, 10, 200, 32);
[view addSubview:segmentButton];
结果不是我所期望的。原始 .png 图像大约 100 像素高,并且它们没有缩小以适合分段控件的 32 像素高度。这会导致分段控件被绘制,巨大的图像与其重叠:
我怎样才能告诉控件缩小这些图像图像?
How can I scale down the images used in a UISegmentedControl
? I am creating the segmented control programmatically:
UISegmentedControl * segmentButton;
segmentButton = [UISegmentedControl segmentedControlWithItems:
[NSArray arrayWithObjects:
[UIImage imageNamed:@"option_one.png"],
[UIImage imageNamed:@"option_two.png"],
nil]];
segmentButton.contentMode = UIViewContentModeScaleToFill;
segmentButton.frame = CGRectMake(10, 10, 200, 32);
[view addSubview:segmentButton];
The result is not what I expect. The original .png images are about 100 pixels high, and they are not scaled down to fit the 32-pixel height of the segmented control. This results in a segmented control being drawn with enormous images overlapping it:
How can I tell the control to scale down those images?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您永远不应该使用“大”图像来仅显示小图片。完整图像将加载到内存中,并且仅显示其像素的 10%,因此您将徒劳地使用大量内存。
如果您确实想使用此资源,您可以做的就是使用之前的代码创建一个缩略图,然后使用这个新生成的缩略图。
以下方法返回一个可以在 UISegmentedControl 中使用的新图像,并且可以释放大图像。
用你的代码:
You should never use a "big" image to display only a small picto. The full image will be loaded in memory, and only 10% of its pixels will be displayed, so you will use a lot of memory for nothing.
What you can do if you really want to use this resource is create a thumbnail with code before, and use this new generated thumbnail.
The following method returns a new image you can use in your UISegmentedControl, and you can release the big one.
With your code:
在斯威夫特3中,
In swift3,
对于 iOS 14 及更高版本
UIGraphicsGetImageFromCurrentImageContext
使一些图像不太清晰,所以我更改为UIGraphicsImageRenderer
,它对我来说效果很好。For iOS 14 and later
UIGraphicsGetImageFromCurrentImageContext
make some image not that clear, so I change toUIGraphicsImageRenderer
, it works fine to me.