user1688346
user1688346

Reputation: 1970

2x image not working on retina device

I have an image with the following naming convention. It always shows the non 2x version on retina device. I had removed cache images from derived data but still not showing. It works if i explicitly set the imageNamed to "Back" These are the images. [email protected] Back.png

UIImage *backImage = [UIImage imageNamed:@"Back"];
NSLog(@"back image height %f",backImage.size.height);
NSLog(@"back image width %f",backImage.size.width);

UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
[btnBack setImage:backImage forState:UIControlStateNormal];
btnBack.frame = CGRectMake(0, 0, backImage.size.width, backImage.size.height);
[btnBack addTarget:self action:@selector(Click_On_Btn_Back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:btnBack];
self.navigationItem.leftBarButtonItem = backBarButton;

Upvotes: 1

Views: 1674

Answers (4)

thavasidurai
thavasidurai

Reputation: 2002

@Keller's answer should work. are you sure that you are using png images.

Upvotes: 0

Anton
Anton

Reputation: 4018

This is just one approach, but you can get the path from the bundle:

// in this example, we load test.png from the bundle
NSString *pathToResource = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"png"];

What makes this so convenient is that it not only automatically determines whether to use the @2x version of your image, but also when you have localized files, this provides the path the the file for the current user locale. This is handy since localized files are not in the main directory, but are rather in their own subfolders (for example, English localized files are in the @"en.lproj" subfolder), and calling them by name is a hassle because you need the full path. This method gets the path for you.

Upvotes: 0

DrAL3X
DrAL3X

Reputation: 799

You should not specify .png in imageNamed: parameter.

Example: files should be names Back.png and [email protected]; Your code should be

UIImage *backImage = [UIImage imageNamed:@"Back"];

Upvotes: 0

Keller
Keller

Reputation: 17081

Try

[UIImage imageNamed:@"Back.png"];

Also, make sure both versions of the image are listed under Target->Build Phases->Copy Bundle Resources. If the @2x version is not, make sure when you import it you check the box "Copy into destination group's folder (if needed)."

Upvotes: 1

Related Questions