5

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

替代文字
(来源:partywithvika.com

替代文字
(来源: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];

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

4

7 回答 7

8

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

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

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

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

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

于 2010-03-22T00:25:45.760 回答
3

I had this problem and it was driving me nuts. After some investigation it turned out that my image was smaller than the frame, and hence the scaling up blurred it. Once I had put in higher resolution images the problem is gone.

Make sure your image size is greater than your UIImageView frame size.

于 2010-03-13T12:44:04.140 回答
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);
于 2010-04-12T05:58:12.700 回答
1

I just had the same problem. First I thought wtf do I need glasses?

Then I realized it´s just not possible to center an image (or label) when you give it an odd number. You need to resize your image to e.g. 46*46 if you want to center it and stay sharp.

Or you can leave it at 45*45 but then you need to decide whether you want your image to be mal-centered 1 pixel to the right, or 1 pixel to the left. (or leave it blurry).

于 2011-10-31T13:25:38.587 回答
1

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

contentMode = UIViewContentModeScaleAspectFit;
于 2009-08-10T14:00:27.307 回答
0

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

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

于 2009-08-09T09:46:42.810 回答
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)];
于 2012-01-13T12:19:43.210 回答