Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 612
A. G Avatar asked Aug 22 '17 08:08

A. G


People also ask

How do I assign an image in Swift?

First one is to drag and drop an image file from Finder onto the assets catalog. Dragging the image will automatically create new image set for you. Drag and drop image onto Xcode's assets catalog. Or, click on a plus button at the very bottom of the Assets navigator view and then select “New Image Set”.

How do I add an image to UIImageView?

The first step is to drag the UIImageView onto your view. Then open the UIImageView properties pane and select the image asset (assuming you have some images in your project). You can also configure how the underlying image is scaled to fit inside the UIImageView.

How do I put an image in Xcode?

Create a New Image Set To create an image set, generate an image asset outside of Xcode, then import it into an asset catalog. The system converts the image format into the most appropriate representation when you build the project. In the Project navigator, select an asset catalog: a file with a .

How do I display an image in SwiftUI?

You can display an image in a SwiftUI view by using the Image view. First you need to add the image to a new image set in your Assets. xcassets file in the Xcode project navigator.


1 Answers

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

like image 56
Kiran Jadhav Avatar answered Oct 14 '22 07:10

Kiran Jadhav