【问题标题】:Resize image in UITabBarItem programmatically?以编程方式调整 UITabBarItem 中的图像大小?
【发布时间】:2014-03-03 22:22:06
【问题描述】:

我有这个代码,将图像放在 barItem 中:

frist.tabBarItem.image = [UIImage imageNamed:@"1.jpg"];

效果很好,但是图像没有出现,因为图像有 256x256 像素,我听说图像必须有 30x30 像素,但我不想手动更改分辨率,我想更改这个每个代码,可能吗?

【问题讨论】:

    标签: ios objective-c uitabbaritem


    【解决方案1】:

    试试这样的:

    UIImage *image = [UIImage imageNamed:@"1.jpg"];
    
    [image drawInRect:CGRectMake(0, 0, 30, 30)];
    
    frist.tabBarItem.image = image;
    

    【讨论】:

    • 一个问题,如果我这样做,ios 正在读取 256x256 的图像并将其调整为 30x30,但这会消耗更多内存吗?
    • 这将消耗更多内存,因为它正在加载 UIImage 然后调整它的大小。另一种方法是查看 ImageIO.framework。 stackoverflow.com/questions/5860215/…
    【解决方案2】:
    let moreVC = storyboard.instantiateViewController(withIdentifier: "MoreVC")
    moreVC.tabBarItem = UITabBarItem(title: "More", image: UIImage(named: "fifth"), tag: 5)
    moreVC.tabBarItem.imageInsets.top = 7
    moreVC.tabBarItem.imageInsets.bottom = 7
    

    【讨论】:

    • 您能否提供一些上下文说明为什么这段代码 sn-p 解决了大小问题? TIA。
    猜你喜欢
    • 1970-01-01
    • 2011-01-12
    • 1970-01-01
    • 2017-03-23
    • 2017-07-12
    • 1970-01-01
    • 2021-04-28
    • 2011-07-08
    • 1970-01-01
    相关资源
    最近更新 更多