简体   繁体   中英

Having @2x image assets without @1x assets

I'm working on an app which has a lot of graphics. The app is targeting iPhones with iOS 7+, so all of the devices that support it have retina displays. I'm adding only @2x and @3x graphics, because @1x graphics are never going to be used, and I don't want the size of the bundle to grow more than it has to.

I load most of the images programmatically:

[self.imageView setImage:[UIImage imageNamed:@"ButtonOn"]];

This is supposed to load ButtonOn@2x.jpg for @2x displays and ButtonOn@3x.jpg for iPhone 6 plus.

However, I noticed this behavior:

  • iPhone 6 Plus does load the @3x image.
  • But the other iPhones – 4, 4s, 5s and 6, do not load the @2x.
  • I can force it to load the @2x image by using the graphics full name, for instance: [UIImage imageNamed:@"ButtonOn@2x.jpg"] , but this defeats the purpose of the naming convention, and it also forces me to detect the screen density by myself.

Is there a way to have @2x and @3x images without @1x ones?

UPDATE: the problem had nothing to do with the screen density. The problem was that, since I am using JPG graphics instead than PNG ones, Xcode can't find them unless I use the file's full name extension. Maybe this is a bug.

There is nothing wrong with omitting the 1x image, and indeed it is usual to do so for an iPhone-only app (as there are no single-resolution iPhone devices that will run this system). The problem is your code. In particular, this line is wrong:

[self.imageView setImage:[UIImage imageNamed:@"ButtonOn"]];

You have two choices:

  • This is a .jpg file, so say so:

     [self.imageView setImage:[UIImage imageNamed:@"ButtonOn.jpg"]]; 
  • Even better, just leave it the way you have it, and use an asset catalog instead of individual files with the funny @2x names.

EDIT : Based on your comments and the details in your question, I'd have to say that you are deeply confused about whether or not you are using an asset catalog. You say that you are, but in that case, saying [UIImage imageNamed:@"ButtonOn@2x.jpg"] would not work — and you say that it does work. So clearly you still have this file sitting at the top level of your app bundle. That's part of your problem. You need to delete these image files from the app bundle and rely solely on the asset catalog.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM