views:

877

answers:

5
A: 

Try to align your IconObj view at screen pixels. If it's really 45x45 then you should set the center to something like CGPointMake(x + 0.5f, y + 0.5f).

You should also double check image size in pixels (e.g in Preview.app Command-I).

Nikolai Ruhe
Nikolai, I didn't get what you mean by 'x' and 'y' in your comment. The interesting thing is that UIImageView is very sharp if initialized with bigger picture with the same code.
Denis M
With x and y I mean any integer coordinates. While the frame, bounds and center properties of views use CGFloat based types for all coordinates, it's usually a good idea to pixel-align the views to the screen.
Nikolai Ruhe
A: 

What I ended up doing is loading bigger picture into 45x45 UIImageView, of course with

contentMode = UIViewContentModeScaleAspectFit;
Denis M
I told you to double check image size.
Nikolai Ruhe
Nikolai, my image was 45x45 and frame for the UIImageView was also 45x45. Where do you see a potential to misinterpret this setup?
Denis M
Did you try to align the pixels of the image to screen pixels?
Nikolai Ruhe
Nikolai is correct: your image dimensions are both odd (45), and setting the `center` property to an integer point means that your origin is offset to half a pixel (0.5). This will result in a blurry image every time. When setting the center, you must check each dimension and adjust by 0.5 if that dimension is odd.
Steve Madsen
A: 

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.

Pillis
+1  A: 

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.

adkap
Most of the other answers (aside from the original poster's) recognise this is a pixel alignment problem, however +1 for this as it's the cleanest and most general purpose. solution
MrCranky
+2  A: 

Your IconObj is 45 pixels wide. You move your IconObj center to 265 which makes its frame to (242.5, VERTICAL_POINT_ICON - 25*0.5, 45, 25). Image will always be blur if some of frame parameter is not integer.

Solution, calculate the frame parameter yourself (don't use center). And always make it integer (cast to NSInteger, use ceil, floor, or round).

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