一個(gè)小坑,關(guān)于TabBarItem.image圖的設(shè)置艳丛,如果需要從網(wǎng)上下載圖再賦值郭计,必須先給它賦值一張默認(rèn)的圖 才能在下載完成的block中修改成功,整了我一上午播聪!
如下:
self.homeVC.tabBarItem.image = [[UIImage imageNamed:defaultItem.icon] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.homeVC.tabBarItem.selectedImage = [[UIImage imageNamed:defaultItem.selectedIcon] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//使用SDWebImage來(lái)下載及緩存
[[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:responseItem.icon] options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
if (!error && finished) {
self.homeVC.tabBarItem.image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
}];
[[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:responseItem.selectedIcon] options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
if (!error && finished) {
self.homeVC.tabBarItem.selectedImage = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
}];
經(jīng)試驗(yàn)朽基,雖然后面block中會(huì)給tabBarItem.image賦值,但如果前面先不賦個(gè)默認(rèn)值离陶,圖片會(huì)無(wú)法顯示的稼虎,所以前面這句一定不能掉:self.homeVC.tabBarItem.image = [[UIImage imageNamed:defaultItem.icon] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
另外
不借助SDWebImage,也可以使用下方法異步下載圖片顯示(無(wú)緩存)
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *imageString = @"http://www.easyicon.net/api/resizeApi.php?id=1199824&size=48";
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageString]];
UIImage *image = [UIImage imageWithData:data];
dispatch_async(dispatch_get_main_queue(), ^{
self.imageView.image = image;
});
});