Getting EXC_BAD_ACCESS when using UIImage(contentsOfFile: )

131 Views Asked by At

I am currently using UIImage(contentsOfFile: path) if a function to return image of a file path but it is getting me EXC_BAD_ACCESS. I have searched a lot and found some things about auto releasing and solutions in Objective-C but I couldn't find anything for Swift. What should I do in Swift?

1

There are 1 best solutions below

0
levan On

Check for nil:

let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
let nsUserDomainMask = FileManager.SearchPathDomainMask.userDomainMask
let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
if let dirPath = paths.first {
   let imageURL = URL(fileURLWithPath: dirPath).appendingPathComponent("Image2.png")
   let image    = UIImage(contentsOfFile: imageURL.path)
   // Do whatever you want with the image
}