Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Images not showing in iOS 7.1 with XCode 6 / Swift

Tags:

xcode

swift

ios7

I have this ObjC code:

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

This works great with these combinations:

  • XCode 5, iOS 7.1
  • XCode 6, iOS 7.1
  • XCode 6, iOS 8

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.

like image 920
pudge Avatar asked Jun 05 '14 15:06

pudge


5 Answers

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")
like image 172
mgcm Avatar answered Oct 06 '22 11:10

mgcm


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

like image 40
John Kakon Avatar answered Oct 06 '22 10:10

John Kakon


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

like image 3
Anton Gaenko Avatar answered Oct 06 '22 10:10

Anton Gaenko


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/.

like image 2
A-Live Avatar answered Oct 06 '22 11:10

A-Live


@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
like image 1
johnboiles Avatar answered Oct 06 '22 11:10

johnboiles