2

I am using UITabBar and UITabBarItem. I have an URL of an image. I set UITabBarItem's image to that image using URL. But image is not showing up. If I use any other image from my MacBook, it works. My URL is correct, which I verified by copy pasting in browser. Below is my code. Can anyone see any problem?

UIImage * iconImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:singleMatch.imageUrl]]];
// add UITabBarItem to an array
[tabs addObject:[[UITabBarItem alloc] initWithTitle:singleMatch.realName image:[self convertImage:iconImage toSize:CGSizeMake(40, 30)] tag:i]];
[self.chatTabBar setItems:tabs animated:YES];

I use below method to resize the image to fit in UITabBarItem // resizes given image to specified CGSize

- (UIImage *)convertImage:(UIImage *)image toSize:(CGSize)size
{
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage * resizedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return resizedImage;
}
Geek
  • 8,280
  • 17
  • 73
  • 137
  • `NSURL URLWithString` will not download an image. Take a look at `NSURLDownload` to see the steps for downloading content (https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html#//apple_ref/doc/uid/20001836-BAJEAIEE) – bobnoble May 02 '13 at 12:42
  • @bobnoble it downloads image. I verified by displaying it in an UIImageView. So that is not the problem. – Geek May 03 '13 at 04:53

2 Answers2

2

I solved my problem by using this method.
- (void)setFinishedSelectedImage:(UIImage *)selectedImage withFinishedUnselectedImage:(UIImage *)unselectedImage
Here, selectedImage is shown when UITabBarItem is selected and inselectedImage is shown when it is not selected.

Geek
  • 8,280
  • 17
  • 73
  • 137
0

see this for your question, just implement it it is easy to use

TabbarWithCenterButton

SimpleTabbar

ALL Tabbar for reference. you can choose any one.

SAMIR RATHOD
  • 3,512
  • 1
  • 20
  • 45