pudge
pudge

Reputation: 448

Images not showing in iOS 7.1 with XCode 6 / Swift

I have this ObjC code:

[self.myButton setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@-button", self.myObject.name]]
                         forState:UIControlStateNormal];

This works great with these combinations:

But when I port one single class of the project to Swift -- a class which is unrelated to anything that is happening in this code -- the images do not display in XCode 6 for iOS 7.1. It does work with XCode 6 and iOS 8.

I finished porting the entire project to Swift, so now the code looks like:

self.myButton.setBackgroundImage(
    UIImage(named:self.myObject.name + "-button"),
    forState: UIControlState.Normal
)

And it's still unhappy on iOS 7.1. Still no images. (The custom icon works, though.) Yes, I know this is just beta software, and it's probably just a bug ... ? But I'm just wondering if anyone has a solution or insight.

I only started using XCode and ObjC about a week ago (surprise! ObjC is now deprecated!), so it could be that I am missing something, but since it works in ObjC, and in Swift+iOS8, it seems like it's probably a bug.

Upvotes: 21

Views: 13452

Answers (7)

Sixto Saez
Sixto Saez

Reputation: 12680

If you are attempting to use an external bundle with proper PNG images stored in either an "old style" folder or inside xcassets then apps running on iOS 7 will not be able to access these images.

I tried a suggestion from another answer here to add the "old style" folder path of the images in the external bundle but that didn't work for me. My solution, which fit my scenario, was to expose an outlet for the resource in the nib inside the external bundle. This allowed the main app to set with an accessible image to it. Whether this is a bug or simply a constraint in using nibs contained in external bundles with iOS 7 will matter less as it rides on to the usage sunset...

PS: Apps running on iOS 8 where able to access images in the external bundle xcassets just fine so choose your poison for supporting iOS 7.

Upvotes: 0

Tiago A.
Tiago A.

Reputation: 2586

I had a problem where some images in xcassets worked and others didn't (only for iOS 7.1). I solved it by deleting the problematic image sets and creating new ones with different names.

Upvotes: 0

Anton Gaenko
Anton Gaenko

Reputation: 9015

For me it was because I suddenly add jpg image to Image Assets. Just resaved it as png and all work well

Upvotes: 3

John Kakon
John Kakon

Reputation: 2541

Fixed in XCode 6 beta 2

Images from asset catalogs in projects with a minimum deployment target of iOS 7 or OS X 10.9 will be available when running on iOS 8 and OS X 10.10, and now also iOS 7 and OS X 10.9. (17029658)

release xcode 6 beta 2 notes

Upvotes: 4

johnboiles
johnboiles

Reputation: 3524

@o KB o points out here that the Xcode6 Beta release notes mention that xcassets bundles aren't supported for iOS7. Additionally, I've found it surprisingly hard to get rid of asset bundles in your project. This can cause naming collisions if your images have the same name as images that were previously in the bundle (you probably want the same names, so you don't have to rename your images everywhere in code / IB).

Here's a workaround:

  1. Copy each image out of your .xcassets image bundle to a new directory (let's call it Images/). See below for a script to make this easier.
  2. Delete your .xcassets bundle. (surprisingly, removing it from the project isn't enough. In my testing, if the .xcassets bundle was anywhere in the same directory as the Xcode project or related sources, it would get copied in. Alternately, you can remove the .xcassets extension)
  3. Add all your image files to your Xcode project
  4. Clean (cmd + shift + k)
  5. Delete the app from the target device or simulator
  6. Install and run

To make step #1 less tedious, I wrote a script to copy images out of the .xcassets bundle and into a directory of your choice: https://github.com/johnboiles/xcasset_exporter

mkdir Images
./xcasset_exporter.py MyProject/Images.xcassets Images

Upvotes: 1

mgcm
mgcm

Reputation: 841

I've managed to overcome this by not using Asset Libraries to store my images.

Add your images to the project using the "old style naming", like Image.png and [email protected], then load them without any extensions in the filename, as such:

let myImage = UIImage(named:"Image")

Upvotes: 9

A-Live
A-Live

Reputation: 8944

I guess you are testing it on simulator. Make sure the desired image is copied to 7.1 bundle. To do it check copy resources bundle build stage or check manually app bundles for different simulators at ~/Library/Application Support/iPhone Simulator/.

Upvotes: 2

Related Questions