11

I am trying to implement using Images.xcassets into a project I am working on. From what I understand I can just put all the different sized images for different devices in there and then call [UIImage imageNamed:@"name_of_image_set"] and it will return the correct image for the device I am working on.

It seems to be pulling the correct image for everything except the iPhone 5/5s/5c with the 4" screen. For that screen size it gives me the image for the @2x iPhone with the 3.5" screen.

Image of config in Images.xcassets

Here is the json that is included in the folder with the images.

{
  "images" : [
    {
      "idiom" : "iphone",
      "scale" : "1x",
      "filename" : "bg.png"
    },
    {
      "idiom" : "iphone",
      "scale" : "2x",
      "filename" : "bg@2x.png"
    },
    {
      "idiom" : "iphone",
      "filename" : "bg-568h@2x.png",
      "subtype" : "retina4",
      "scale" : "2x"
    },
    {
      "idiom" : "ipad",
      "scale" : "1x",
      "filename" : "bg~ipad.png"
    },
    {
      "idiom" : "ipad",
      "scale" : "2x",
      "filename" : "bg@2x~ipad.png"
    }
  ],
  "info" : {
    "version" : 1,
    "author" : "xcode"
  }
}

Everything seems to be in order, am I just misunderstanding how xcassets are supposed to work?

Thanks

Joel Bell

Joel Bell
  • 2,718
  • 3
  • 26
  • 32
  • possible duplicate of [New image name for iPhone 5](http://stackoverflow.com/questions/12497776/new-image-name-for-iphone-5) – JRG-Developer Sep 27 '13 at 23:29
  • I've seen that question, this one is asking specifically about xcode 5 and the new asset management functionality. What is the point of being able to include 4" iphone screen assets into an "image set" if there is no way to get it out again? – Joel Bell Sep 27 '13 at 23:49
  • You sure? I haven't gone back and looked at it in a while, but I implemented it just fine on Xcode 5 – Joel Bell Jan 31 '14 at 18:29

2 Answers2

4

I came across this problem and the issue seems to be targeting iOS versions lower than 7.0 The solution for me was to create a separate image set with a single @2x image on it and instantiate the correct one programmatically by detecting the iPhone screen size in code like done here

Related: Why isn't my Asset Catalog returning R4 images?

Community
  • 1
  • 1
0

I had the same issue but only in ios7 and I load the images programmatically, but it should be the same problem.

In my viewDidLoad I added:

if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) { // if iOS 7
    self.edgesForExtendedLayout = UIRectEdgeNone; //layout adjustements
}

It basically just recognizes iOS7 and applies some layout adjustements. After I added this code, the correct picture got selected. Finally I load my image, which you don't have to do:

 [productview setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]]];

Found this answer also here in stackoverflow, but didn't find it anymore.

Charles
  • 439
  • 3
  • 16
  • Well, I tried and doesn't work. Actually the method you're using to detect iOS version ain't good at all. If you're targeting 6.1, [UIImage imagenamed:] is unable to pick the right R4 image... – Alejandro Benito-Santos Mar 27 '14 at 09:46
  • http://blog.ittybittyapps.com/blog/2013/11/08/working-with-ios-6-and-7/ Check ios6 runtime exception, same method to check for IOS7. – Charles Mar 27 '14 at 15:25
  • Oh and Uiimage imagenamed works for sure: http://stackoverflow.com/questions/3442239/retina-icons-2x-arent-being-used-when-images-are-specified-in-code werent in 4.0 but that was a bug. – Charles Mar 27 '14 at 15:31
  • That blog post is incorrect, this is the right, recommended by Apple way to check iOS version: http://roadfiresoftware.com/2014/01/the-right-way-to-check-the-ios-version-in-an-app/ Furthermore, as I explained before [UIImage imageNamed:] isn't picking the right image for retina 4" display when you're using an .xcassets file. The SO link you posted is irrelevant for the matter – Alejandro Benito-Santos Mar 27 '14 at 15:37