从服务器下载的UITabbar项目图像显示模糊,但如果使用资产文件夹中的相同图像,则效果很好

问题描述 投票:0回答:1

我已经为我的应用程序创建了 UITabbar,根据要求,所有信息都将来自 API。收到回复后,我已下载图像并设置为 UITabbar Item。 我使用以下代码来下载图像。

for (int i=0; i<[tabbarItemImageArray count]; i++) {

    NSURLSession *session = [NSURLSession sharedSession];
    [[session dataTaskWithURL:[tabbarItemImageArray objectAtIndex:i]
              completionHandler:^(NSData *data,
                                  NSURLResponse *response,
                                  NSError *error) {
        
        UIImage *imagemain=[UIImage imageWithData:data];
        
        dispatch_async(dispatch_get_main_queue(), ^(){
            
            if (i < 5) {
                
                if (imagemain != nil) {


                    UIImage *resizedImage = [[WSUtils sharedObject] resizeImageForTabbarItem:imagemain scaledToSize:CGSizeMake(25,25)];

                    self.tabBarController.tabBar.items[i].image = [resizedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
               self.tabBarController.tabBar.items[i].selectedImage = resizedImage;
                }
                
                self.tabBarController.tabBar.items[i].title = [tabbarItemNameArray objectAtIndex:i];
                self.tabBarController.tabBar.unselectedItemTintColor = [UIColor colorWithRed:35/255.0 green:63/255.0 blue:136/255.0 alpha:1.0];
            }
            
        });

      }] resume];

tabbarItemImageArray 是包含图像 url 的 NSURL 对象的数组。我已经以编程方式创建了 UITabbarController 。这些是我用过的属性。

self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.delegate = self;
self.tabBarController.view.backgroundColor = [UIColor whiteColor];
self.tabBarController.viewControllers = tabbarArray;
self.tabBarController.tabBar.backgroundColor = [UIColor whiteColor];
self.tabBarController.tabBar.layer.shadowColor = [UIColor blackColor].CGColor;
self.tabBarController.tabBar.layer.shadowOffset = CGSizeMake(0.0, 0.0);
self.tabBarController.tabBar.layer.shadowRadius = 5;
self.tabBarController.tabBar.layer.shadowOpacity = 0.3;
self.tabBarController.tabBar.layer.masksToBounds = NO;
self.tabBarController.tabBar.layer.cornerRadius = 30;
self.tabBarController.tabBar.backgroundImage = [[UIImage alloc] init];
self.tabBarController.tabBar.layer.maskedCorners = kCALayerMinXMinYCorner | kCALayerMaxXMinYCorner;
self.tabBarController.selectedIndex = 0;

这将给出以下结果。请参阅以红色突出显示 UITabbar。

如果我下载图像并将其添加到 Xcode 中的图像资源并运行应用程序,它会显示干净的图像。见下图。

我花了一整天的时间来研究这个问题,但什么也没发现。任何帮助都会很感激,即使是很快的。

更新: 我尝试过 25x25、50x50 和 75x75 图像。我已经一一上传了所有 3 个尺寸的图像并尝试过,但它不起作用。我附上 50x50 图像的示例图标。我在上面的屏幕截图中使用了它。

我已将标签栏项目的 50x50 图像调整为 25x25,因为 50x50 太大而无法放入标签栏。以下方法是我用来调整大小的。

-(UIImage*) resizeImageForTabbarItem:(UIImage*)image scaledToSize:(CGSize)newSize {

float width = newSize.width;
    float height = newSize.height;

    UIGraphicsBeginImageContext(newSize);
    CGRect rect = CGRectMake(0, 0, width, height);

    float widthRatio = image.size.width / width;
    float heightRatio = image.size.height / height;
    float divisor = widthRatio > heightRatio ? widthRatio : heightRatio;

    width = image.size.width / divisor;
    height = image.size.height / divisor;

    rect.size.width  = width;
    rect.size.height = height;

    //indent in case of width or height difference
    float offset = (width - height) / 2;
    if (offset > 0) {
        rect.origin.y = offset;
    }
    else {
        rect.origin.x = -offset;
    }

    [image drawInRect: rect];

    UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return smallImage;

}

ios objective-c uitabbaritem nsurlsessiondownloadtask
1个回答
0
投票

我已经找到解决办法了。调整实际导致问题的图像大小。如果您使用以下方法,它将在保持图像质量的情况下调整图像大小。

extension UIImage {

func resize(targetSize: CGSize) -> UIImage {
    return UIGraphicsImageRenderer(size:targetSize).image { _ in
        self.draw(in: CGRect(origin: .zero, size: targetSize))
    }
}

}

我从以下链接找到了答案。 https://stackoverflow.com/a/49060676/2384760

© www.soinside.com 2019 - 2024. All rights reserved.