Reputation: 9418
Code written in Swift for displaying UIImages works in the iOS 8.0 simulator but not on a phone running IOS 7.0 for some reason.
let image1 = UIImage(named: "img1")
let imageview = UIImageView(image: image1)
self.view.addSubview(imageview)
let image2 = UIImage(named: "img2")
let button = UIButton(frame: CGRect(origin: CGPoint(x: 0, y: 100), size: image2.size)
button.setImage(image2, forState: UIControlState.Normal)
self.view.addSubview(button)
The images are in the right place with the right size and valid references, they're just not being displayed, except on the simulator running iOS 8.
Is this a problem for anyone else?
EDIT: Deleting from Images.xcassets, cleaning the project and copying the files into the bundle itself seems to work for me.
Upvotes: 28
Views: 60252
Reputation: 11547
One possible reason is height. Check the height of UIImageView, might be higher in code or in storyboard.
Upvotes: 0
Reputation: 2657
Swift 5:
The accepted answer no longer compiles for various reasons, so here is my updated version:
let image1 = UIImage(named: "img1.jpg")
if let myImage1 = image1 {
let imageview = UIImageView(image: myImage1)
self.view.addSubview(imageview)
} else {
print ("img1 not loaded")
}
let image2 = UIImage(named: "img2.jpg")
if let myImage2 = image2 {
let button = UIButton(frame: CGRect(origin: CGPoint(x: 0, y: 100), size: myImage2.size))
button.setImage(myImage2, for: .normal)
self.view.addSubview(button)
} else {
print ("img2 not loaded")
}
Upvotes: 0
Reputation: 12034
You would check if the image was added to your framework, if that is unchecked you are not able to load the resource, but if you check it, the image will be loaded
Upvotes: 5
Reputation: 511558
There seems to be a disconnect between the votes for the question and the other answers, so I will add a generic answer for how to display an image in Swift. That is what originally brought me here anyway.
Use UIImage
to get a reference to the image in the main bundle.
let image = UIImage(named: "rocket.png")
imageView.image = image
Notes
imageView
is a UIImageView
Upvotes: 3
Reputation: 11
I stored my images in the " Images.xcassets" folder and my images didn't appear on my device but they did appear on the simulator. In order to display the "Images.xcassets" images on my device (iPhone 5), I, the solution that worked for me was to copy bundle resources for the "Images.xcassets" folder
To Copy Bundle Resources: Select Target>Build Phases>Copy Bundle Resources>Select "+" (Add Items)>Select "Images.xcassets
Upvotes: 1
Reputation: 134
Inorder to display images you have to change the code in following way
let image1 = UIImage(named: "img1.jpg")
let imageview = UIImageView(image: image1)
self.view.addSubview(imageview)
let image2 = UIImage(named: "img2.jpg")
let button = UIButton(frame: CGRect(origin: CGPoint(x: 0, y: 100), size: image2.size)
button.setImage(image2, forState: UIControlState.Normal)
self.view.addSubview(button)
Remember swift is not supporting .png format images, It is supporting .jpg format.
Upvotes: 10
Reputation: 3485
I tried your code in iOS 8-Simulator and on my iOS 7 device and it did what it should do. So no, in my case I don't see a problem.
I changed img1 to img1.JPG (name of file) and img2 to img2.png. And I copied the images directly into the folder where the view controller is..
Upvotes: 3