我正在使用UITabBar
和UITabBarItem
。我有一个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;
}