Search code examples
iphoneiosuitabbar

Loading retina images to tabbar dynamically


The images I'm using in the tabbar are dynamic and I load them off the internet. I detect if I'm on retina display or not and download the correct images and initialize the tabbar using them.

Normal images are working fine. My problem is with retina images. They are displayed in big size, bigger than the tabbar, so they are not completely shown.

I wonder how should I load retina images into the tabbar and make them look normal.

Update:

Here's the code I'm using to load the images to the tabbar from each view controller added:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString* path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithString:@"1.png"]];
    UIImage* image = [UIImage imageWithContentsOfFile:path];

It doesn't matter if the images have @2x or not as I'm choosing normal or retina images manually.


Solution

  • I haven't run across this problem myself, but I'd start by setting a breakpoint just after you create the image. Use the debugger to check the new image's scale property. I think you'll find that scale is set to 1.0, but your high resolution images should have scale set to 2.0. There are three ways (that I know of) to make that happen:

    • Name the file with a "@2x" suffix;
    • Set the 'dpi' attribute for your image appropriately;
    • Create the image with -initWithCGImage:scale:orientation: or +imageWithCGImage:scale:orientation: