Search code examples
swift3uiimageview

How to set an image in imageview swift 3?


So I have an image view titled cashOrCredit and have am trying to set it's image programatically but somehow not able to.

First I set the image like this

cell.cashOrCredit.image = UIImage(named: "cash1.png")

and I get an error saying a separator of "," is needed.

Then I tried it this way

var cashImage: UIImage?
cashImage = "cash1.png"
cell.cashOrCredit.image = cashImage

But I get a THREAD 1 EXC BAD INSTRUCTION error. I can't seem to understand what is going wrong ?

Here is the error

1


Solution

  • Updated for Swift 3:

    use below simple code, to set the image to UIImageView;

    class YourViewControllerName: UIViewController {
    
            var mYourImageViewOutlet: UIImageView?
    
            func addImageToUIImageView{
                var yourImage: UIImage = UIImage(named: "Birthday_logo")!
                mYourImageViewOutlet.image = yourImage
            } // call this function where you want to set image.
         }
    

    Note: "Birthday_logo" type of image must be present in your Assets.xcassets of your project. I attached the screenshot if you want any help please refer it.

    ****// used anywhere you want to add an image to UIImageView. [Here I used one function & in that function, I write a code to set image to UIImageView]****

    Enjoy..!enter image description here