为什么我的UIImageView模糊了?

Den*_*kov 5 iphone objective-c blur uiimageview

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

替代文字http://partywithvika.com/iconinsim.png 替代文字http://partywithvika.com/iconinxcode.png

我有这个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;
}
Run Code Online (Sandbox Code Playgroud)

我像这样创建它:

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];
Run Code Online (Sandbox Code Playgroud)

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

Ada*_*lan 8

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

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

desertIconView.center = CGPointMake(265,VERTICAL_POINT_ICON);
desertIconView.frame = CGRectIntegral(desertIconView.frame);
Run Code Online (Sandbox Code Playgroud)

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

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


Nik*_*uhe 0

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

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