UIImageView Blurs Image

I have a really weird problem with UIImageView. I have an image (RGB PNG) of 45x45 pixels, which I add to the view. I see that the image is blurry after adding 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 a 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 should the displayed image be the one that is stored in the file?

+5
source share
7 answers

, , iPhone UIKit, . ( ). , , , center. center, x, y, ... .

CGRectIntegral , :

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

, , frame , center, , .

: ! , ... .

+8

, . , , , , . , .

, 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);
+3

45x45 UIImageView,

contentMode = UIViewContentModeScaleAspectFit;
+1

. , ?

, ( ), . , . 46 * 46, .

45 * 45, , , 1 1 . ( ).

+1

Try aligning the view IconObjin pixels on the screen. If it is really 45x45, then you should set the center to something like CGPointMake (x + 0.5f, y + 0.5f).

You should also double-check the image size in pixels (for example, in Preview.app Command-I).

0
source

full source for bias problem:

  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)];
0
source

All Articles