28

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.

5
  • 2
    Try to change the "img1" to "img1.png" (add the image type) Commented Jun 4, 2014 at 8:29
  • set frame of imageview Commented Jun 4, 2014 at 8:34
  • 2
    I'm having a problem with Swift and UIImages also. For some reason in a project with Objective-C and Swift doesn't find UIImages from the application bundle when loaded with UIImage(named: ...) and running the app on iOS 7.1.1. Commented Jun 4, 2014 at 17:42
  • 1
    I followed the EDIT instructions. Important to note "Cleaning the project" is required. Just a build and run is not enough. My iPhone 5 is running iOS7.1.1 and I also had to move my images out of the xcassets file and into the project itself Commented Jun 7, 2014 at 6:10
  • I don't want the text version of this feature, I want the GUI version. Whenever I clicked the class UIImage a little square would pop up and then I would choose graphically which image to pu there. Commented Sep 23, 2022 at 23:46

7 Answers 7

10

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.

Sign up to request clarification or add additional context in comments.

4 Comments

PNG works in Swift, just not in Images.xcassets for me.
Swift support both in playground, if it is PNG, you needed let image1 = UIImage(named: "img1"), if it is jpg, you needed let image1 = UIImage(named: "img1.jpg")
This is the correct solution to the problem of images.xcassets defining the extension of the image. Warning: if you add the images as part of the images.xcassets and also in a different folder in your solution you are literally adding 2 of the same image in the project and it is not good for your app in the long run.
The answer doesn't state clearly what has been changed. All that has been changed is that .jpg has been added to the end of each file name. The code no longer compiles in Swift 5 for three reasons. I will add a version that compiles below.
5

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 enter image description here

Comments

3

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

6 Comments

An iPhone 5 with as I said iOS7 (needed more characters for this answer.. :D
which base SDK is your project using?
The iOS 8 SDK and deployment target is iOS 7.0. But you have to use iOS 8 SDK for swift don't you?
as far as I can tell, yeah. my settings are the same but it still won't display on my device. very strange
Ah.. so copying the image to the main folder worked only after also deleting it from images.xcassets. I guess xcassets is the problem. Thanks for the help
|
3

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.

How to display an image in Swift

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
  • In my tests, including or excluding the .png file extension did not make a different. It might be necessary to include if you are using multiple image formats, though.
  • Also in my tests, it did not make a difference whether the image was directly added to the project or was in Assets.xcassets.
  • If you need an image that is not in the main bundle, see this question.
  • According to the edit in the original question here, deleting the image and re-adding it to the bundle is what solved the problem.

Comments

1

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

1 Comment

My Swift SpriteKit project included an Assets.xcassets image manager automatically. Here I wrote about constructing Sprites or SKSpriteNodes from those images: stackoverflow.com/questions/20480145/…
0

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")
    }

Comments

0

One possible reason is height. Check the height of UIImageView, might be higher in code or in storyboard.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.