Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImageView blurs image

I have a really weird problem with UIImageView. I have an image (an RGB png) 45x45 pixels which I add to the view. I can see that image is blurred after added to the view. Here is the same image in the simulator (left) and in Xcode (right):

alt text
(source: partywithvika.com)

alt text
(source: partywithvika.com)

I have custom UIImageView class with this initWithImage code:

- (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;
}

I create it like this:

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];

Why would the displayed image be so than the one stored in a file?

like image 644
Denis Masyukov Avatar asked Aug 09 '09 04:08

Denis Masyukov


1 Answers

In general, everything you do on iPhone with UIKit should be pixel-aligned. Problems with pixel alignment usually manifest as blurriness (this is especially true with text and images). This is why when I find blurry view, I first check if I'm setting the center property. When you set center, the frame's x, y, height and width are adjusted around that center point... frequently resulting in fractional values.

You could try using CGRectIntegral on the frame as shown:

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

This may work, but if it doesn't, try setting the frame manually, without using center property to ensure that no values are fractional.

Edit: Whoops! Didn't notice that the OP had answered his own question... I'll leave this up for informational reasons anyway.

like image 60
Adam Kaplan Avatar answered Sep 19 '22 20:09

Adam Kaplan