[imageName] couldn’t be opened because the text encoding of its contents can’t be determined

Hi all,


I’ve got some code that accesses the camera and lets the user take a photo. The photo then shows up in the UIImagepickercontroller. That all works well. What I’m trying to do is save the photo as a temp image to the /var/mobile/containers/data/application/[long id]/documents/imageName folder. I’m not sure if the save is successful or not, because when I try to get the fileURL of the image I’m getting the error:


“tempImage” couldn’t be opened because the text encoding of it’s contents can’t be determined.”


Overall, what I’m trying to do is just assign a name to a photo the user has just taken so I can send that name to a global variable and reference it elsewhere. I assume to assign a name to a photo I have to save it first, so I’m trying to save it to the app’s documents folder.


Here’ my code snippet – it runs inside the func imagePickerController


let documentDirectory = try fileManagewr.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create:false)
let fileURL = documentDirectory.appendingPathComponent("tempImage")
if let imageData = UIImageJPEGReporesentation(pickedImage, 0.5) {
     try imageData.write(to: fileURL)
     do {
          let contents = try String(contentsOf: fileURL)
          // myString is my global variable
          myString = contents
     } catch {
          print (error)


Any help on this is greatly appreciated!


Powered by WPeMatico


You may also like...

Comments are closed.