Resizing an image in a UITableViewCell should not be the total height

I'm currently trying to show a simple table in my iPhone application, where I use UITableViewCell with the style UITableViewCellStyleValue1 (image on the left, label with label on the right). All cells have a default height (50.0f). Before adding the image to the cell, I resized the image to 40x40, so this is not the total height of the cell (I think it looks ugly).

I am doing this with this code:

cell.imageView.image = [UIImage imageNamed:@"icon.png"]; cell.imageView.image = [RootViewController imageWithImage:cell.imageView.image scaledToSize:CGSizeMake(40, 40)]; 

It is all very pleasant and works flawlessly. But I want to do this on the iPhone 4 (with a higher resolution screen). The problem is that everything scales seamlessly on the iPhone 4, but the images seem very pixelated.

The reason for this is that everything on the screen is blown up to scale for a new resolution, as well as images, so the images should probably be something like 80x80. But when I resize them to 80x80 (originals 120x120), they look like big, due to scaling.

Is there a way to make my images not full table height, but I want them to be in higher resolution on iPhone 4. Should I create a complete new view for this?

Unfortunately, after the first answer, I realized that my own written function is missing:

 + (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize { UIGraphicsBeginImageContextWithOptions(newSize, NO, [[UIScreen mainScreen] scale]); [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } 

As you can see, after the first answer, I tried to get this to work with the UIGraphicsBeginImageContextWithOptions method, but somehow this leads to an empty image.

+4
source share
1 answer

I assume you wrote "imageWithImage: scaledToSize:", right?

I also assume that in this call you are using "UIGraphicsBeginImageContext (yourSize)". Replace this with "UIGraphicsBeginImageContextWithOptions (yourSize, NO, 2.0)" if your platform is iPhone 4.

"2.0" defines the scale factor for the points (you determine the size in points not in pixels). On the display in front of the grid, the dot is 1x1 pixels. On the retina display, the dot is 2x2 pixels.

Edit: Make sure you have the high-resolution version of "icon.png" in your resources called " icon@2x.png ". It automatically loads if it is a retina screen.

+5
source

All Articles