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.

Marcello B.
  • 4,177
  • 11
  • 45
  • 65
Joseph Mark
  • 9,298
  • 4
  • 29
  • 31
  • 2
    Try to change the "img1" to "img1.png" (add the image type) – gran33 Jun 04 '14 at 08:29
  • set frame of imageview – iPatel Jun 04 '14 at 08: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. – Markus Rautopuro Jun 04 '14 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 – Derek Knight Jun 07 '14 at 06: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. – victorkolis Sep 23 '22 at 23:46

7 Answers7

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.

CNU
  • 134
  • 3
  • 3
    PNG works in Swift, just not in Images.xcassets for me. – Joseph Mark Jun 10 '14 at 07:48
  • 7
    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") – Kuroro Jun 14 '14 at 08:21
  • 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. – S.H. Sep 15 '14 at 18:23
  • 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. – paulo62 Mar 21 '20 at 13:17
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

Benjamin RD
  • 11,516
  • 14
  • 87
  • 157
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..

Ben
  • 3,455
  • 3
  • 26
  • 31
  • An iPhone 5 with as I said iOS7 (needed more characters for this answer.. :D – Ben Jun 04 '14 at 08:38
  • which base SDK is your project using? – Joseph Mark Jun 04 '14 at 08:44
  • The iOS 8 SDK and deployment target is iOS 7.0. But you have to use iOS 8 SDK for swift don't you? – Ben Jun 04 '14 at 08:46
  • 1
    as far as I can tell, yeah. my settings are the same but it still won't display on my device. very strange – Joseph Mark Jun 04 '14 at 08:47
  • And you are absolutely sure that you copied the images into the project and they are part of the target? Are they in the images.xcassets? – Ben Jun 04 '14 at 08:48
  • 2
    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 – Joseph Mark Jun 04 '14 at 08:59
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.
Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
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

  • My Swift SpriteKit project included an Assets.xcassets image manager automatically. Here I wrote about constructing Sprites or SKSpriteNodes from those images: http://stackoverflow.com/questions/20480145/fill-skshapenode-with-pattern-image/33666529#33666529 – Jerry Frost Nov 12 '15 at 07:54
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")
    }
paulo62
  • 2,637
  • 1
  • 21
  • 15
0

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

Saranjith
  • 11,242
  • 5
  • 69
  • 122