1

I have this code who, put an image inside a barItem:

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

It works perfectly, but the image does not appear, because the image have 256x256 pixels, I've heard that the image must have a 30x30 pixels, but I don't wanna to change resolution manually, I want to change this per code, is possible?

DrummerB
  • 39,814
  • 12
  • 105
  • 142
user3372120
  • 134
  • 1
  • 10

2 Answers2

3

Try something like this:

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

[image drawInRect:CGRectMake(0, 0, 30, 30)];

frist.tabBarItem.image = image;
user3258468
  • 354
  • 7
  • 18
  • One question, if i do this, the ios is reading a image 256x256 and rezise this for 30x30, but this consume more memory no? – user3372120 Mar 03 '14 at 22:29
  • This will consume more memory, since it is loading the UIImage and then resizing it. Another way is to look at ImageIO.framework. http://stackoverflow.com/questions/5860215/resizing-a-uiimage-without-loading-it-entirely-into-memory – user3258468 Mar 03 '14 at 22:34
0
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
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • 3
    Can you provide some context on why this code snippet solves the sizing problem? TIA. – benc Sep 14 '20 at 11:28