-3

There are many examples on how to use imageWithContentsOfFile in object-c, but how do I use it in Swift. imageView.image = UIImage(imageWithContentsOfFile: imageName) returns Argument labels '(imageWithContentsOfFile:)' do not match any available overloads

while if I write: imageView.image = UIImage(contentsOfFile:imageName) It doesn't display the image.

(I have all images stores in Media.xcassets) I am displaying a lot of images so using UIImage(named:imageName) isn't a viable option. How can I get the functionality of contentsOfFile in swift?

Tarjerw
  • 505
  • 1
  • 5
  • 12

1 Answers1

-1

Swift code:

func getImage (named name : String) -> UIImage?
{
    if let imgPath = Bundle.main.path(forResource: name, ofType: ".png")
    {
        return UIImage(contentsOfFile: imgPath)
    }
    return nil
}

//Image name "MyImage.png"

getImage(named: "MyImage")!
YakovL
  • 7,557
  • 12
  • 62
  • 102
AtulParmar
  • 4,358
  • 1
  • 24
  • 45
  • Please explain what is the code supposed to do and how it is supposed to help. They are specifically asking about `imageWithContentsOfFile` which you don't mention at all. Best regards – YakovL Mar 26 '18 at 16:25