UIImageView模糊图像

问题描述 投票:5回答:7

我对UIImageView有一个非常奇怪的问题。我有一个45x45像素的图像(RGB png),我添加到视图中。添加到视图后,我可以看到图像模糊。这是模拟器(左)和Xcode(右)中的相同图像:

alt text (来源:partywithvika.com

alt text (来源:partywithvika.com

我有这个initWithImage代码的自定义UIImageView类:

- (id) initWithImage:(UIImage*) image {
    self = [super initWithImage:image];

    self.frame = CGRectMake(0, 0, 45, 45);
    self.contentMode = UIViewContentModeScaleAspectFit;

    self.quantity = 1;
    if (self) {
        self.label = [[UITextField alloc]initWithFrame:CGRectMake(0,40,45,25)];
        self.label.font = [UIFont systemFontOfSize:16];
        self.label.borderStyle = UITextBorderStyleNone;
        self.label.enabled = TRUE;
        self.label.userInteractionEnabled = TRUE;
        self.label.delegate = self;
        self.label.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        self.label.textAlignment = UITextAlignmentCenter;
    }
    self.userInteractionEnabled = TRUE;

    // Prepare 3 buttons: count up, count down, and delete
    self.deleteButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.deleteButton.hidden = NO;
    self.deleteButton.userInteractionEnabled = YES;
    self.deleteButton.titleLabel.font  = [UIFont systemFontOfSize:20];
    self.deleteButton.titleLabel.textColor = [UIColor redColor];
    [self.deleteButton setTitle:@"X" forState:UIControlStateNormal];
    [self.deleteButton addTarget:self action:@selector(deleteIcon:) forControlEvents:UIControlEventTouchUpInside];

    self.upCountButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.upCountButton.hidden = NO;
    self.upCountButton.userInteractionEnabled = YES;
    [self.upCountButton setTitle:@"+" forState:UIControlStateNormal];
    [self.upCountButton addTarget:self action:@selector(addQuantity:) forControlEvents:UIControlEventTouchUpInside];

    self.downCountButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.downCountButton.hidden = YES;
    self.downCountButton.userInteractionEnabled = YES;
    [self.downCountButton setTitle:@"-" forState:UIControlStateNormal];
    [self.downCountButton addTarget:self action:@selector(removeQuantity:) forControlEvents:UIControlEventTouchUpInside];
    return self;
}

我像这样创建它:

UIImage *desertIcon = [UIImage imageNamed:@"desert.png"];
IconObj *desertIconView = [[IconObj alloc] initWithImage:desertIcon];
desertIconView.center = CGPointMake(265,VERTICAL_POINT_ICON);
desertIconView.type = [IconObj TYPE_DESERT];
[self.view addSubview:desertIconView];
[desertIconView release];

为什么显示的图像会比存储在文件中的图像多?

objective-c iphone uiimageview blur
7个回答
8
投票

一般来说,你在iPhone上使用UIKit所做的一切都应该是像素对齐的。像素对齐的问题通常表现为模糊(对于文本和图像尤其如此)。这就是为什么当我找到模糊的视图时,我首先检查我是否设置了center属性。设置center时,框架的x,y,高度和宽度围绕该中心点进行调整...经常导致小数值。

您可以尝试在框架上使用CGRectIntegral,如下所示:

desertIconView.center = CGPointMake(265,VERTICAL_POINT_ICON);
desertIconView.frame = CGRectIntegral(desertIconView.frame);

这可能有效,但如果没有,请尝试手动设置frame,而不使用center属性以确保没有值是小数。

编辑:哎呀!没有注意到OP已经回答了他自己的问题......无论如何,我会出于提供信息的原因留下这个问题。


3
投票

我有这个问题,它让我疯了。经过一番调查后发现我的图像比图像小,因此放大模糊了它。一旦我放入更高分辨率的图像,问题就消失了。

确保您的图像大小大于UIImageView框架大小。


3
投票

你的IconObj是45像素宽。您将IconObj中心移动到265,使其框架为(242.5,VERTICAL_POINT_ICON - 25 * 0.5,45,25)。如果某些帧参数不是整数,则图像将始终模糊。

解决方案,自己计算帧参数(不要使用中心)。并始终使其为整数(强制转换为NSInteger,使用ceil,floor或round)。

desertIconView.frame = CGRectMake((NSInteger)(265 - 45*0.5),
                                  (NSInteger)(VERTICAL_POINT_ICON - 25*0.5),
                                  45, 25);

1
投票

我最终做的是将更大的图片加载到45x45 UIImageView中,当然还有

contentMode = UIViewContentModeScaleAspectFit;

1
投票

我刚遇到同样的问题。首先我认为wtf我需要眼镜吗?

然后我意识到当你给它一个奇数时,不可能将图像(或标签)居中。您需要将图片大小调整为例如46 * 46如果你想让它居中并保持锋利。

或者您可以将其保留为45 * 45但是您需要决定是否要将图像偏向右侧1个像素,或者左侧1个像素。 (或让它模糊)。


0
投票

尝试将屏幕像素对齐IconObj视图。如果真的是45x45那么你应该将中心设置为CGPointMake(x + 0.5f,y + 0.5f)。

您还应该以像素为单位仔细检查图像大小(例如,在Preview.app Command-I中)。


0
投票

完成偏移问题的来源:

  contentImage  = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];

  UIImage *cImage = [UIImage imageNamed:image];
  self.contentImage.image = cImage;

  [self.contentImage setFrame:CGRectMake(0, 0, cImage.size.width, cImage.size.height)];

  CGFloat offset = 0.0f;

  if ( ( (int)cImage.size.width % 2 ) && ( (int)cImage.size.height % 2 ) ) {
    offset = 0.5f;
  }

  [self.contentImage setCenter:CGPointMake(256.0f + offset, 256.0f + offset)];
© www.soinside.com 2019 - 2024. All rights reserved.