Set url to local path UImage - crashes

Setting up images in collection view with local path url like this:

let fileURL = documentsUrl.appendingPathComponent(fileName)
myImageView.image  =  UIImage(named: fileURL.path)

      

The reasons for the crashes - high quality images - also tried to reduce the size - still the application crashes without an error message.

+3


source to share


2 answers


func getDirectoryPath() -> String {
    let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
    let documentsDirectory = paths[0]
    return documentsDirectory
}

let imagePath = (getDirectoryPath() as NSString).appendingPathComponent("/\(file_name).jpg")

let myImageView.image = UIImage(contentsOfFile: imagePath)!

      



0


source


If the image is bundled (included in the app), you are not passing the entire path to UIImage(named: ...)

, only the filename. So your challenge will beUIImage(named: fileName)



If you have an image in the document path (you downloaded it from the app) use UIImage(contentsOfFile: fileURL.path)

0


source







All Articles