David Sanford
David Sanford

Reputation: 745

Issue with file manager in swift 3.1

I have the below code that worked fine, up until the release of swift 3.1.

func loadImage() {

    id = userPhotoModel.id

    let fileManager = FileManager.default

    let imagePath = (self.getDirectoryPath() as NSString).appendingPathComponent(photoName)

    if fileManager.fileExists(atPath: imagePath){
        let imageFromPath = resizeImage(named: (contentsOfFile: imagePath))

        print("name of photo retrieved: \(photoName)")

        self.userPhoto.image = imageFromPath

    }else{
        print("No Image")
    }
}

Now, swift 3.1 wants to add as! String to:

let imageFromPath = resizeImage(named: (contentsOfFile: imagePath) as! String)

However, when I run the app, it crashes in this location with no error message as per the below image.

What is causing this?

enter image description here

EDIT: Here is the resizeImage func

fileprivate func resizeImage(named name: String) -> UIImage
{

    var image = UIImage(named: name)

    if image!.size.height > image!.size.width
    {
        self.userPhoto.contentMode = .scaleAspectFit
    }
    else
    {
        image = image!.resizeTo(self.userPhoto.bounds)
    }
    return image!
}

Upvotes: 0

Views: 101

Answers (1)

rmaddy
rmaddy

Reputation: 318814

The issue is the confusing syntax in the line:

let imageFromPath = resizeImage(named: (contentsOfFile: imagePath))

That should simply be:

let imageFromPath = resizeImage(named: imagePath)

No cast required and correct in any Swift 3.x.

Upvotes: 1

Related Questions