Joel Bell
Joel Bell

Reputation: 2718

iPhone 4" screen with xcassets not pulling correct image

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" : "[email protected]"
    },
    {
      "idiom" : "iphone",
      "filename" : "[email protected]",
      "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

Upvotes: 11

Views: 7586

Answers (2)

Alejandro Benito-Santos
Alejandro Benito-Santos

Reputation: 2069

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?

Upvotes: 4

Charles
Charles

Reputation: 414

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.

Upvotes: 0

Related Questions