views:

54

answers:

2

I need to load localized images in my iOS app, but also have to take into account that they might need to be the ...@2x kind. How can I do this?

+1  A: 

NSLocalizedString should do the trick. See Localizing String Resources.

UIImage *img = [UIImage imageNamed:NSLocalizedString(@"TestImage",@"")];
catlan
No, I mean the image is localized, not the name. I have a resource in `en.lproj` and one in `es.lproj`, etc.
Alexsander Akers
I'm not sure how to simple load the image when it's in a *.lproj folder. Using the method above you would have all the image in your resource folder with a suffix like TestImage-es.png and map them in the string file.
catlan
A: 

Use the methods of NSBundle to fetch the path of the image and then load it using UIImage's imageWithContentsOfFile method. I added a method to UIImage for this, like:

@implementation UIImage (UIImageAdditions)

+ (UIImage*)localizedImageNamed:(NSString*)name
{
  NSString* path = [[NSBundle mainBundle] pathForResource:name ofType:@"png"];
  return [self imageWithContentsOfFile:path];
}

@end
Peyman