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.
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")
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
For me it was because I suddenly add jpg image to Image Assets. Just resaved it as png and all work well
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/
.
@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:
.xcassets
image bundle to a new directory (let's call it Images/
). See below for a script to make this easier..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)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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With