UISegmentedControl图像缩放

发布于 2024-10-15 13:54:21 字数 712 浏览 2 评论 0原文

如何缩小 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 像素高度。这会导致分段控件被绘制,巨大的图像与其重叠:

screen shot

我怎样才能告诉控件缩小这些图像图像?

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:

screen shot

How can I tell the control to scale down those images?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一杆小烟枪 2024-10-22 13:54:21

您永远不应该使用“大”图像来仅显示小图片。完整图像将加载到内存中,并且仅显示其像素的 10%,因此您将徒劳地使用大量内存。

如果您确实想使用此资源,您可以做的就是使用之前的代码创建一个缩略图,然后使用这个新生成的缩略图。

以下方法返回一个可以在 UISegmentedControl 中使用的新图像,并且可以释放大图像。

- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    
    UIGraphicsEndImageContext();
    return newImage;
}

用你的代码:

UISegmentedControl * segmentButton;
segmentButton = [UISegmentedControl segmentedControlWithItems: [NSArray arrayWithObjects:
                  [self imageWithImage:[UIImage imageNamed:@"option_one.png"] scaledToSize:CGSizeMake(32, 32)],
                  [self imageWithImage:[UIImage imageNamed:@"option_two.png"] scaledToSize:CGSizeMake(32, 32)],
                  nil]];
segmentButton.contentMode = UIViewContentModeScaleToFill;
segmentButton.frame = CGRectMake(10, 10, 200, 32);
[view addSubview:segmentButton];

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.

- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    
    UIGraphicsEndImageContext();
    return newImage;
}

With your code:

UISegmentedControl * segmentButton;
segmentButton = [UISegmentedControl segmentedControlWithItems: [NSArray arrayWithObjects:
                  [self imageWithImage:[UIImage imageNamed:@"option_one.png"] scaledToSize:CGSizeMake(32, 32)],
                  [self imageWithImage:[UIImage imageNamed:@"option_two.png"] scaledToSize:CGSizeMake(32, 32)],
                  nil]];
segmentButton.contentMode = UIViewContentModeScaleToFill;
segmentButton.frame = CGRectMake(10, 10, 200, 32);
[view addSubview:segmentButton];
她说她爱他 2024-10-22 13:54:21

在斯威夫特3中,

extension UIImage {
    func scaleImage(scaleToSize: CGSize) -> UIImage {
        UIGraphicsBeginImageContext(scaleToSize)

        self.draw(in: CGRect(origin: CGPoint(x: 0, y: 0), size: scaleToSize))
        let newImage = UIGraphicsGetImageFromCurrentImageContext()

        UIGraphicsEndImageContext()
        return newImage!
    }
}

In swift3,

extension UIImage {
    func scaleImage(scaleToSize: CGSize) -> UIImage {
        UIGraphicsBeginImageContext(scaleToSize)

        self.draw(in: CGRect(origin: CGPoint(x: 0, y: 0), size: scaleToSize))
        let newImage = UIGraphicsGetImageFromCurrentImageContext()

        UIGraphicsEndImageContext()
        return newImage!
    }
}
夏见 2024-10-22 13:54:21

对于 iOS 14 及更高版本

UIGraphicsGetImageFromCurrentImageContext 使一些图像不太清晰,所以我更改为 UIGraphicsImageRenderer,它对我来说效果很好。

输入图片此处描述

extension UIImage {
    func resizedImage(to targetSize: CGSize) -> UIImage? {
        let render = UIGraphicsImageRenderer(size: targetSize)
        return render.image { ctx in
            self.draw(in: .init(origin: .zero, size: targetSize))
        }
    }
}

class SomeView: UIView {
    func initSegementedControl() {
        exampleSegmenedControl = UISegmentedControl(items:[
            leftImage.resizedImage(to: .init(width: 15, height: 15))!,
            rightImage.resizedImage(to: .init(width: 15, height: 15))!
        ])
    }    

}

For iOS 14 and later

UIGraphicsGetImageFromCurrentImageContext make some image not that clear, so I change to UIGraphicsImageRenderer, it works fine to me.

enter image description here

extension UIImage {
    func resizedImage(to targetSize: CGSize) -> UIImage? {
        let render = UIGraphicsImageRenderer(size: targetSize)
        return render.image { ctx in
            self.draw(in: .init(origin: .zero, size: targetSize))
        }
    }
}

class SomeView: UIView {
    func initSegementedControl() {
        exampleSegmenedControl = UISegmentedControl(items:[
            leftImage.resizedImage(to: .init(width: 15, height: 15))!,
            rightImage.resizedImage(to: .init(width: 15, height: 15))!
        ])
    }    

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文