2

我正在使用UITabBarUITabBarItem。我有一个URL of an image. 我使用 URL 将 UITabBarItem 的图像设置为该图像。但是图像没有显示出来。如果我使用 MacBook 中的任何其他图像,它就可以工作。我的网址是正确的,我通过在浏览器中复制粘贴来验证。下面是我的代码。任何人都可以看到任何问题吗?

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];

我使用以下方法调整图像大小以适合 UITabBarItem // 将给定图像调整为指定的 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;
}
4

2 回答 2

2

我通过使用这种方法解决了我的问题。
- (void)setFinishedSelectedImage:(UIImage *)selectedImage withFinishedUnselectedImage:(UIImage *)unselectedImage
在这里,selectedImage 在选择 UITabBarItem 时显示,在未选择时显示 inselectedImage。

于 2013-05-03T05:58:56.447 回答
0

看到这个你的问题,只需实现它很容易使用

TabbarWithCenterButton

简单标签栏

所有标签栏供参考。你可以选择任何一个。

于 2013-05-02T13:01:20.427 回答